473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

memcpy( dest, src, 0 )

given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard as far
as I can tell. So presumably they do have to be valid even when the size is
0.
Nov 14 '05 #1
8 15814
Spacen Jasset wrote:
given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard as far
as I can tell. So presumably they do have to be valid even when the size is
0.


They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.

--
Er*********@sun.com

Nov 14 '05 #2
Spacen Jasset <sp**********@yahoo.co.uk> wrote:

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard as far
as I can tell.


7.21.1p2:

Where an argument declared as size_t n specifies the length of
the array for a function, n can have the value zero on a call to
that function. Unless explicitly stated otherwise in the
description of a particular function in this subclause, pointer
arguments on such a call shall still have valid values, as
described in 7.1.4.

-Larry Jones

I stand FIRM in my belief of what's right! I REFUSE to
compromise my principles! -- Calvin
Nov 14 '05 #3
Eric Sosman wrote:
Spacen Jasset wrote:
given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard
as far
as I can tell. So presumably they do have to be valid even when the
size is
0.

They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.


Are there any examples of the latter?

--
Pull out a splinter to reply.
Nov 14 '05 #4
Peter Ammon <ge******@mac.com> wrote:
Eric Sosman wrote:
Spacen Jasset wrote:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid?
<snip> They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.


Are there any examples of the latter?


Passing a null pointer to free(), for one.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
In <W7*****************@newssvr27.news.prodigy.com> Peter Ammon <ge******@mac.com> writes:
Eric Sosman wrote:
Spacen Jasset wrote:
given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard
as far
as I can tell. So presumably they do have to be valid even when the
size is
0.


They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.


Are there any examples of the latter?


A null pointer as argument is undefined behaviour for *most* library
functions taking pointers as parameters, according to the above mentioned
chapter and verse. But have a look at fflush(), setbuf(), free(),
realloc(), time(), snprintf(), perror(), strto*(), strxfrm() (and maybe
others I cannot think of right now).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Peter Ammon wrote:
Eric Sosman wrote:
Spacen Jasset wrote:
given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard
as far
as I can tell. So presumably they do have to be valid even when the
size is
0.


They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.


Are there any examples of the latter?


Others have pointed out some functions that accept NULL
arguments. Here's a possibly odder example:

char notstring[13] = "Hello, world!"; // no trailing zero
printf ("%.6s strange new world!\n", notstring);

The "%s" specifier usually takes an argument that points to
the start of a zero-terminated C string, but when a precision
is specified the argument need only point to the start of an
array of `char', not necessarily zero-terminated.

There's actually a practical application for this. I once
found myself working with a system whose customary way of
representing string data was not to use a C-style terminator,
but to use a count. To make this mesh with C one had to append
a zero byte after the counted characters, and this could get
clumsy because a system-provided string probably didn't have
any extra room on the end -- you had to malloc() a one-larger
buffer, memcpy() the counted data into it, and *then* stuff a
zero byte in order to get a C-style string. However, if all
you wanted to do was print the thing out:

printf ("The answer is %.*s\n", count, pointer);

worked like a charm.

--
Er*********@sun.com

Nov 14 '05 #7
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
Peter Ammon wrote:
Eric Sosman wrote:
Spacen Jasset wrote:

given:

memcpy( dest, src, 0 )

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard
as far
as I can tell. So presumably they do have to be valid even when the
size is
0.

They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.


Are there any examples of the latter?


Others have pointed out some functions that accept NULL
arguments. Here's a possibly odder example:

char notstring[13] = "Hello, world!"; // no trailing zero
printf ("%.6s strange new world!\n", notstring);

The "%s" specifier usually takes an argument that points to
the start of a zero-terminated C string, but when a precision
is specified the argument need only point to the start of an
array of `char', not necessarily zero-terminated.


I must be missing something. What is the chapter and verse from which
your example is an exception?!? What makes your example strange in any
way?

s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8

<la************@ugsplm.com> wrote in message
news:8p************@jones.homeip.net...
Spacen Jasset <sp**********@yahoo.co.uk> wrote:

What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard as far as I can tell.


7.21.1p2:

Where an argument declared as size_t n specifies the length of
the array for a function, n can have the value zero on a call to
that function. Unless explicitly stated otherwise in the
description of a particular function in this subclause, pointer
arguments on such a call shall still have valid values, as
described in 7.1.4.

-Larry Jones

I stand FIRM in my belief of what's right! I REFUSE to
compromise my principles! -- Calvin


Right, thanks. That's cleared it up. I didn't come across that bit.
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
11
by: Peter Pichler | last post by:
A colleague encountered an interesting problem. Suppose we have a C function like this: void WRITE_THING(void* addr, THING t) { memcpy(addr, &t, sizeof t); } to copy a THING to any byte...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
20
by: gert | last post by:
This based on a example i found at http://www.cs.tut.fi/~jkorpela/ forms/cgic.html #include <fcgi_stdio.h> #include <stdlib.h> int urldecode(char *src, char *last, char *dest){ int code;...
2
by: ziibrs | last post by:
Is it true that it is not allowed to overwrite statically initialized char * ? For example: #include <stdio.h> #include <string.h> int main(void) { char *dest=" ";
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
11
by: mthread | last post by:
Hi, I would like to know if C++ has a class equivalent to memcpy and its associated functions available in C.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.