Re: Is memcpy with len=0 a NOP?



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

Noob wrote:

Consider

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.

4.11.2.1 The memcpy function

Synopsis

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

Description

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.

Returns

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 | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
.



Relevant Pages

  • Re: Is memcpy with len=0 a NOP?
    ... int i = 123; ... to a NOP, leaving the entire state machine unchanged? ... PUSH ... ... A call to memcpy() with 0 for the ...
    (comp.lang.c)
  • Re: Is memcpy with len=0 a NOP?
    ... int i = 123; ... I believe memcpyis indeed equivalent to a NOP. ... does not fully define what memcpy does when the length is 0. ... The memcpy function copies n characters from the object pointed to ...
    (comp.lang.c)
  • Re: Is memcpy with len=0 a NOP?
    ... int i = 123; ... I believe memcpyis indeed equivalent to a NOP. ... The memcpy function copies n characters from the object pointed to ...
    (comp.lang.c)
  • Is memcpy with len=0 a NOP?
    ... int i = 123; ... I believe memcpyis indeed equivalent to a NOP. ... The memcpy function ... The memcpy function copies n characters from the object pointed to ...
    (comp.lang.c)
  • Re: Function pointer assignment and memseting - Whats happening here?
    ... I'm loading an a.out file on memory, ... specially on how I couldn't find an equivalent line using memcpy. ... no automatic conversion from a function pointer to an `int': ...
    (comp.lang.c)