Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar) the same?

Discussion in 'C Programming' started by Jonathan Bartlett, Jul 7, 2005.

  1. Re: Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar)the same?

    adelfino wrote:
    > I mean:
    >
    > unsigned char foo[bar] = "";
    >
    > is the same as:
    >
    > unsigned char foo[bar];
    > memset (foo, '\0', bar);


    I believe the former only sets the first character to \0 while the
    second will set all of them.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, Jul 7, 2005
    #1
    1. Advertising

  2. Jonathan Bartlett

    adelfino Guest

    I mean:

    unsigned char foo[bar] = "";

    is the same as:

    unsigned char foo[bar];
    memset (foo, '\0', bar);

    ?

    Thanks!
     
    adelfino, Jul 7, 2005
    #2
    1. Advertising

  3. Hi,

    > ?


    No. They're not the same.

    > unsigned char foo[bar] = "";


    According to ANSI C90 this shouldn't work at all.

    unsigned char foo[bar];
    generates an array of unsigned char with size bar on the stack.

    unsigned char foo = "";
    generates an (const) array of unsigned char consisting of '\0' and
    nothing else.

    > unsigned char foo[bar];
    > memset (foo, '\0', bar);


    memset() sets _all_ bytes of foo to '\0'.

    best regards,
    Stephan

    --
    Stephan Beyer, PGP 0xFCC5040F, IRC sbeyer (seebyr, bseyer)

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD4DBQFCzXLRbt3SB/zFBA8RAjFZAKDXic+4JVuHaB0hWt29NlMfkUnwGACWJzs4
    0pf+WHz1IZy5vSVhUB5ZEQ==
    =xxXJ
    -----END PGP SIGNATURE-----
     
    Stephan Beyer, Jul 7, 2005
    #3
  4. Jonathan Bartlett

    Me Guest

    adelfino wrote:
    > I mean:
    >
    > unsigned char foo[bar] = "";
    >
    > is the same as:
    >
    > unsigned char foo[bar];
    > memset (foo, '\0', bar);
    >
    > ?


    For this specific example, yes. You can also do:

    unsigned char foo[bar] = { 0 };
    unsigned char foo[bar] = { '\0' };

    However, changing this to a signed char (or plain char if the
    underlying type is signed char) is not guaranteed to be the same thing
    as there may be padding bits. They have the same values, but memcmp may
    not return 0 because it's unspecified what these padding bits contain.
     
    Me, Jul 7, 2005
    #4
  5. Jonathan Bartlett

    Eric Sosman Guest

    Re: Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar)the same?

    Jonathan Bartlett wrote:
    > adelfino wrote:
    >
    >> I mean:
    >>
    >> unsigned char foo[bar] = "";
    >>
    >> is the same as:
    >>
    >> unsigned char foo[bar];
    >> memset (foo, '\0', bar);

    >
    > I believe the former only sets the first character to \0 while the
    > second will set all of them.


    You're entitled to believe whatever you like (have you
    heard about the faked Moon landings?), but you might want to
    put your faith to the test by reading Section 6.7.8 paragraph 21:

    "If there are [...] fewer characters in a string
    literal used to initialize an array of known size
    than there are elements in the array, the remainder
    of the aggregate shall be initialized implicitly the
    same as objects that have static storage duration."

    .... and paragraph 10:

    "[...] If an object that has static storage duration
    is not initialized explicitly, then [...] if it has
    arithmetic type, it is initialized to [...] zero [...]"

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 7, 2005
    #5
  6. Jonathan Bartlett

    Eric Sosman Guest

    Re: Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar)the same?

    Stephan Beyer wrote:

    > Hi,
    >
    >
    >>?

    >
    >
    > No. They're not the same.
    >
    >
    >>unsigned char foo[bar] = "";

    >
    >
    > According to ANSI C90 this shouldn't work at all.


    It depends on what `bar' is. If it's a #define'd
    constant integer expression or a positive-valued enum
    constant, this is just fine for C90. C99 expands the
    universe of acceptable `bar' to include positive-valued
    run-time expressions.

    > unsigned char foo[bar];
    > generates an array of unsigned char with size bar on the stack.


    Assuming `bar' is something acceptable, this defines
    such an array. Where that array resides is not known: it
    might have static storage duration or automatic storage
    duration depending on the context, and in either case it's
    the implementation's business to figure out where to store it.

    > unsigned char foo = "";
    > generates an (const) array of unsigned char consisting of '\0' and
    > nothing else.


    No, it generates a required diagnostic under both C90
    and C99 rules. Pointer-to-`char' and `char' are not compatible
    types.

    >>unsigned char foo[bar];
    >>memset (foo, '\0', bar);

    >
    > memset() sets _all_ bytes of foo to '\0'.


    Yes, assuming an acceptable `bar'.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 7, 2005
    #6
  7. Jonathan Bartlett

    Flash Gordon Guest

    Re: Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar)the same?

    Jonathan Bartlett wrote:
    > adelfino wrote:
    >
    >> I mean:
    >>
    >> unsigned char foo[bar] = "";
    >>
    >> is the same as:
    >>
    >> unsigned char foo[bar];
    >> memset (foo, '\0', bar);

    >
    > I believe the former only sets the first character to \0 while the
    > second will set all of them.


    You believe wrong. The standard explicitly states that the rest of the
    array will be zeroed.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jul 7, 2005
    #7
  8. Me wrote:
    > adelfino wrote:
    > > I mean:
    > >
    > > unsigned char foo[bar] = "";
    > >
    > > is the same as:
    > >
    > > unsigned char foo[bar];
    > > memset (foo, '\0', bar);
    > >
    > > ?

    >
    > For this specific example, yes. You can also do:
    >
    > unsigned char foo[bar] = { 0 };
    > unsigned char foo[bar] = { '\0' };
    >
    > However, changing this to a signed char (or plain char if the
    > underlying type is signed char) is not guaranteed to be the same
    > thing as there may be padding bits.


    It is guaranteed...

    6.2.5p9

    The range of nonnegative values of a signed integer type is a
    subrange of the corresponding unsigned integer type, and the
    representation of the same value in each type is the same...

    Since unsigned char is unpadded, the representation of 0 in plain
    or signed char is also all bits zero. [Because that's its
    representation as an unsigned char.]

    --
    Peter
     
    Peter Nilsson, Jul 8, 2005
    #8
    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. Replies:
    3
    Views:
    404
    John Roth
    Jul 29, 2005
  2. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    418
    JaredGrubb
    Sep 24, 2008
  3. Wejn
    Replies:
    2
    Views:
    133
    Michal
    Nov 29, 2003
  4. Max Williams
    Replies:
    10
    Views:
    218
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    355
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page