Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memcpy( dest, src, 0 )

Reply
Thread Tools

memcpy( dest, src, 0 )

 
 
Spacen Jasset
Guest
Posts: n/a
 
      06-30-2004
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.


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-30-2004
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
lawrence.jones@ugsplm.com
Guest
Posts: n/a
 
      07-01-2004
Spacen Jasset <(E-Mail Removed)> wrote:
>
> 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
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      07-01-2004
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?

--
Pull out a splinter to reply.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      07-01-2004
Peter Ammon <(E-Mail Removed)> wrote:
>Eric Sosman wrote:
>> Spacen Jasset wrote:
>>>
>>> memcpy( dest, src, 0 )
>>>
>>> What happens when the size paramater of memcpy is 0? Do the src and dest
>>> have to be valid?

<snip>
>> 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?


Passing a null pointer to free(), for one.

Regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-01-2004
In <W7LEc.6876$(E-Mail Removed)> Peter Ammon <(E-Mail Removed)> writes:

>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?


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-01-2004
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.

--
(E-Mail Removed)

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-01-2004
In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> writes:

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


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Spacen Jasset
Guest
Posts: n/a
 
      07-01-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Spacen Jasset <(E-Mail Removed)> wrote:
> >
> > 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


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments