Re: Is memcpy with len=0 a NOP?
- From: Dr Nick <3-nospam@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 27 Jan 2011 07:20:48 +0000
"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
.
- Follow-Ups:
- Re: Is memcpy with len=0 a NOP?
- From: Ben Bacarisse
- Re: Is memcpy with len=0 a NOP?
- References:
- Is memcpy with len=0 a NOP?
- From: Noob
- Re: Is memcpy with len=0 a NOP?
- From: J. J. Farrell
- Is memcpy with len=0 a NOP?
- Prev by Date: Re: return values
- Next by Date: inline vs. function pointers
- Previous by thread: Re: Is memcpy with len=0 a NOP?
- Next by thread: Re: Is memcpy with len=0 a NOP?
- Index(es):
Relevant Pages
|