Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #21956

Re: copy size

Message-ID <4FD206A5.32BA@mindspring.com> (permalink)
Date 2012-06-08 10:05 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: copy size
References <03c3ed4d-f3ce-437b-9476-1c512c7772de@o3g2000pby.googlegroups.com> <lnehpqcqe3.fsf@nuthaus.mib.org>

Show all headers | View raw


Keith Thompson wrote:
> 
> sinbad <sinbad.sinbad@gmail.com> writes:
> > is it ok to allow memcpy () to take size 0;
> > or is it a good practice to check for the size
> > and do memcpy only for sizes greater than 0;
> 
> See section 7.24.1 of the C standard
> (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
> is the latest draft):
> 
>     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.
> 
> So yes, it's ok to call memcpy() with a size of 0.
> 
> Note that if either s1 or s2 is a null pointer, the behavior is (I
> believe) undefined.

You believe correctly.
Here is the next sentence from n1570:

  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.

And here is some of 7.1.4:

7.1.4 Use of library functions
1  Each of the following statements applies
   unless explicitly stated otherwise
   in the detailed descriptions that follow: 
     If an argument to a function has an invalid value 
     (such as a value outside the domain of the function,
     or a pointer outside the address space of the program,

     or a null pointer,

     or a pointer to non-modifiable storage
        when the corresponding parameter is not const-qualified)
     or a type (after promotion) 
        not expected by a function with variable number of arguments,
     the behavior is undefined.

-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

copy size sinbad <sinbad.sinbad@gmail.com> - 2012-06-07 22:16 -0700
  Re: copy size Keith Thompson <kst-u@mib.org> - 2012-06-07 22:40 -0700
    Re: copy size David T. Ashley <dashley@gmail.com> - 2012-06-08 09:15 -0400
      Re: copy size Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-08 09:32 -0400
        Re: copy size David T. Ashley <dashley@gmail.com> - 2012-06-08 09:40 -0400
          Re: copy size Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-08 10:31 -0400
          Re: copy size Keith Thompson <kst-u@mib.org> - 2012-06-08 11:53 -0700
          Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 12:11 -0700
            Re: copy size "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-08 12:32 -0700
              Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 20:42 -0700
      Re: copy size pete <pfiland@mindspring.com> - 2012-06-08 10:30 -0400
      Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:47 -0700
    Re: copy size pete <pfiland@mindspring.com> - 2012-06-08 10:05 -0400

csiph-web