perils of memset

Discussion in 'C Programming' started by Mantorok Redgormor, Feb 22, 2004.

  1. When is it that memset caused problems?
    I recall from posts in the past where someone
    used memset in their code that invoked undefined
    behavior.

    What about the following?

    char the_array[10];

    memset(the_array, 'A', sizeof the_array);

    does this invoked undefined behavior
    or can create a trap representation?
    iirc there were some issues involving
    trap representations with memset usage.



    --
    nethlek
    Mantorok Redgormor, Feb 22, 2004
    #1
    1. Advertising

  2. Mantorok Redgormor

    Jack Klein Guest

    On 21 Feb 2004 20:30:02 -0800, (Mantorok Redgormor)
    wrote in comp.lang.c:

    > When is it that memset caused problems?
    > I recall from posts in the past where someone
    > used memset in their code that invoked undefined
    > behavior.
    >
    > What about the following?
    >
    > char the_array[10];
    >
    > memset(the_array, 'A', sizeof the_array);
    >
    > does this invoked undefined behavior
    > or can create a trap representation?
    > iirc there were some issues involving
    > trap representations with memset usage.


    Your particular example is just fine, and has well-defined results.
    Unless you later pass the array to a function that accepts a string,
    since it is not '\0' terminated. You are setting each character in
    your array to 'A' which is guaranteed to be a valid value for a
    character.

    The issue is that when using calloc(), or memset(some_object, 0,
    some_size), you set the memory to all bits 0. But all bits 0 is not
    necessarily what you expect it to be.

    The C standard guarantees that all bits 0 produces a valid value of 0
    for all three of the character types. It also guarantees that it
    produces a valid value of 0 in the all of the C99 optional exact width
    integer types, if the compiler has such types. Technically it does
    not guarantee that it produces valid values of 0 in the rest of the
    "ordinary" integer types ((un)signed short, (un)signed int, (un)signed
    long, (un)signed long long), but there is a defect report to correct
    that in a future update.

    What the C standard does not and never will guarantee is that all bits
    0 produces the value 0.0, or any valid value, in floating point types.
    It also does not guarantee that it produces the value NULL, or any
    valid value, in pointer types.

    There are definitely architectures where a null pointer does not have
    the pattern of all bits 0. There are probably implementations where
    all bits 0 is not the value of 0.0 in floating pointy types.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 22, 2004
    #2
    1. Advertising

  3. In article <>,
    (Mantorok Redgormor) wrote:

    > When is it that memset caused problems?
    > I recall from posts in the past where someone
    > used memset in their code that invoked undefined
    > behavior.
    >
    > What about the following?
    >
    > char the_array[10];
    >
    > memset(the_array, 'A', sizeof the_array);
    >
    > does this invoked undefined behavior
    > or can create a trap representation?
    > iirc there were some issues involving
    > trap representations with memset usage.


    memset itself is fine. In your example, the contents of the_array after
    the memset is fine as well. If you call memset for something that is not
    an array of char, for example an array of int, then the resulting values
    may be trap representations and reading them may cause undefined
    behavior.
    Christian Bau, Feb 22, 2004
    #3
    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. Casey Hawthorne
    Replies:
    14
    Views:
    1,053
    Casey Hawthorne
    Jan 16, 2006
  2. Rhino
    Replies:
    1
    Views:
    515
    Thomas Hawtin
    Apr 14, 2006
  3. k-man
    Replies:
    4
    Views:
    4,512
    Shelley Hebert
    Dec 18, 2003
  4. Mantorok Redgormor

    perils of sizeof

    Mantorok Redgormor, Jan 25, 2004, in forum: C Programming
    Replies:
    4
    Views:
    459
    Jack Klein
    Jan 25, 2004
  5. Nick Daly
    Replies:
    1
    Views:
    372
    Steven D'Aprano
    Aug 5, 2009
Loading...

Share This Page