471,086 Members | 1,199 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

memcpy(0,0,0)

Hi, all.

This question came up in a code review: are null pointers legal as
arguments to memcpy? That is, is the program at the end of this article
strictly conforming? (Of course, I check the FAQ first and could not
find an answer on point.)

I don't have C89 or C99 handy, so I checked draft N869 from here:
http://www.open-std.org/jtc1/sc22/wg...69/n869.txt.gz

I note the following:

7.21.1 String function conventions
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.

7.21.2.1 The memcpy function
void *memcpy(void * restrict s1,
const void * restrict s2,
size_t n);
[But see N869 for complete text].

7.1.4 Use of library functions
... unless explicitly stated otherwise in the
detailed descriptions ...
If an argument to a function has an invalid
value (such as ... a null pointer) ... the behavior
is undefined.

So, by my analysis, 7.1.4 says that you must not pass a null pointer to
any library function unless the 7.X documentation allows it. 7.21.1
reenforces 7.1.4, and does not explicity allow a null pointer. 7.21.2.1
also does not explicitly allow a null pointer.

So, memcpy(0,0,0) invokes undefined behavior?

Regards,

Rob

#include <string.h>
int main(void)
{
memcpy(0,0,0);
return 0;
}
Nov 15 '05 #1
1 4678
remove the CAPS <""usenetABC\"@xyzebeep.org (remove the caps)"> wrote:
This question came up in a code review: are null pointers legal as
arguments to memcpy? That is, is the program at the end of this article
strictly conforming? (Of course, I check the FAQ first and could not
find an answer on point.) So, memcpy(0,0,0) invokes undefined behavior?


Yes.

http://snipurl.com/i0ej

(Note that I asked essentially the same question, two years ago, so
you are not alone in wondering.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by franky.backeljauw | last post: by
16 posts views Thread by Delali Dzirasa | last post: by
6 posts views Thread by Samee Zahur | last post: by
5 posts views Thread by manya | last post: by
35 posts views Thread by Christopher Benson-Manica | last post: by
16 posts views Thread by Amarendra GODBOLE | last post: by
33 posts views Thread by Case | last post: by
6 posts views Thread by myhotline | last post: by
18 posts views Thread by Mark | last post: by

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.