memcpy( dest, src, 0 )

S

Spacen Jasset

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.
 
E

Eric Sosman

Spacen said:
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.
 
L

lawrence.jones

Spacen Jasset said:
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.

7.21.1p2:

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. Unless explicitly stated otherwise in the
description of a particular function in this subclause, pointer
arguments on such a call shall still have valid values, as
described in 7.1.4.

-Larry Jones

I stand FIRM in my belief of what's right! I REFUSE to
compromise my principles! -- Calvin
 
P

Peter Ammon

Eric said:
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?
 
D

Dan Pop

In said:
Are there any examples of the latter?

A null pointer as argument is undefined behaviour for *most* library
functions taking pointers as parameters, according to the above mentioned
chapter and verse. But have a look at fflush(), setbuf(), free(),
realloc(), time(), snprintf(), perror(), strto*(), strxfrm() (and maybe
others I cannot think of right now).

Dan
 
E

Eric Sosman

Peter said:
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.
 
D

Dan Pop

In said:
Peter said:
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.

I must be missing something. What is the chapter and verse from which
your example is an exception?!? What makes your example strange in any
way?

s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type.

Dan
 
S

Spacen Jasset

7.21.1p2:

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. Unless explicitly stated otherwise in the
description of a particular function in this subclause, pointer
arguments on such a call shall still have valid values, as
described in 7.1.4.

-Larry Jones

I stand FIRM in my belief of what's right! I REFUSE to
compromise my principles! -- Calvin

Right, thanks. That's cleared it up. I didn't come across that bit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top