Re: Is memcpy with len=0 a NOP?

"J. J. Farrell" <jjf@xxxxxxxxxx> writes:

Noob wrote:


int i = 123;
int j = 456;
memcpy(&i, &j, 0);

Is the call to memcpy with len==0 well-defined, and equivalent
to a NOP, leaving the entire state machine unchanged?
(i and j unchanged, but everything else too)

I believe memcpy(foo, bar, 0) is indeed equivalent to a NOP.
Please correct me if I'm wrong.

My C89 draft only states

Why do you say "only" there? The text below fully defines how the
function must behave when the length is 0, in just the same way as it
defines how it must behave when the length is 42. The memcpy function


#include <string.h>
void *memcpy(void *s1, const void *s2, size_t n);


The memcpy function copies n characters from the object pointed to
by s2 into the object pointed to by s1 . If copying takes place
between objects that overlap, the behavior is undefined.


The memcpy function returns the value of s1 .

I don't think that text is entirely clear. If n is zero we know from
the first sentence that zero characters are copied. But it's far from
clear to me whether if zero bytes have been copied, "copying" has "taken
place", so I remain unsure from the quoted text whether memcpy(x,x,0) is
defined as doing nothing or undefined.
Online waterways route planner |
Plan trips, see photos, check facilities |