Peter Ammon wrote:
Eric Sosman wrote:
Spacen Jasset wrote:
given:
memcpy( dest, src, 0 )
What happens when the size paramater of memcpy is 0? Do the src and dest
have to be valid? It doesn't say anything about this in the standard
as far
as I can tell. So presumably they do have to be valid even when the
size is
0.
They must be valid. See section 7.1.4, paragraph 1. The
general contract is that all arguments to library functions
must be "real" unless the function's description explicitly
permits "strange" arguments.
Are there any examples of the latter?
Others have pointed out some functions that accept NULL
arguments. Here's a possibly odder example:
char notstring[13] = "Hello, world!"; // no trailing zero
printf ("%.6s strange new world!\n", notstring);
The "%s" specifier usually takes an argument that points to
the start of a zero-terminated C string, but when a precision
is specified the argument need only point to the start of an
array of `char', not necessarily zero-terminated.
There's actually a practical application for this. I once
found myself working with a system whose customary way of
representing string data was not to use a C-style terminator,
but to use a count. To make this mesh with C one had to append
a zero byte after the counted characters, and this could get
clumsy because a system-provided string probably didn't have
any extra room on the end -- you had to malloc() a one-larger
buffer, memcpy() the counted data into it, and *then* stuff a
zero byte in order to get a C-style string. However, if all
you wanted to do was print the thing out:
printf ("The answer is %.*s\n", count, pointer);
worked like a charm.
--
Er*********@sun.com