memcpy( dest, src, 0 )

Discussion in 'C Programming' started by Spacen Jasset, Jun 30, 2004.

  1. 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.
     
    Spacen Jasset, Jun 30, 2004
    #1
    1. Advertising

  2. Spacen Jasset

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jun 30, 2004
    #2
    1. Advertising

  3. Spacen Jasset

    Guest

    Spacen Jasset <> 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
     
    , Jul 1, 2004
    #3
  4. Spacen Jasset

    Peter Ammon Guest

    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.
     
    Peter Ammon, Jul 1, 2004
    #4
  5. Peter Ammon <> 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 ()
    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
     
    Irrwahn Grausewitz, Jul 1, 2004
    #5
  6. Spacen Jasset

    Dan Pop Guest

    In <W7LEc.6876$> Peter Ammon <> 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:
     
    Dan Pop, Jul 1, 2004
    #6
  7. Spacen Jasset

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jul 1, 2004
    #7
  8. Spacen Jasset

    Dan Pop Guest

    In <> Eric Sosman <> 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:
     
    Dan Pop, Jul 1, 2004
    #8
  9. <> wrote in message
    news:...
    > Spacen Jasset <> 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.
     
    Spacen Jasset, Jul 2, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren
    Replies:
    1
    Views:
    790
    Tim Roberts
    Apr 27, 2006
  2. Tim Golden
    Replies:
    0
    Views:
    606
    Tim Golden
    Apr 27, 2006
  3. clu

    memcpy src & dst same

    clu, Oct 25, 2005, in forum: C Programming
    Replies:
    2
    Views:
    1,640
    Richard Bos
    Oct 25, 2005
  4. gert
    Replies:
    20
    Views:
    1,170
  5. ramadevimandala
    Replies:
    0
    Views:
    522
    ramadevimandala
    Apr 16, 2009
Loading...

Share This Page