Is there a problem in passing 0
to memcpy()
:
memcpy(dest, src, 0)
Note that in my code I am using a variable instead of 0
, which can sometimes evaluate to 0
.
As one might expect from a sane interface, zero is a valid size, and results in nothing happening. It's specifically allowed by the specification of the various string handling functions (including memcpy
) in C99 7.21.1/2:
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. [...] On such a call, a function that locates a character finds no occurrence, a function that compares two character sequences returns zero, and a function that copies characters copies zero characters.
Yes, it's totally Ok. The only restriction on memcpy is that memory areas must not overlap.