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

  • Thread starter Jonathan Bartlett
  • Start date
J

Jonathan Bartlett

adelfino said:
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
 
A

adelfino

I mean:

unsigned char foo[bar] = "";

is the same as:

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

?

Thanks!
 
S

Stephan Beyer

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

Me

adelfino said:
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.
 
E

Eric Sosman

Jonathan said:
adelfino said:
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 [...]"
 
E

Eric Sosman

Stephan said:
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'.
 
F

Flash Gordon

Jonathan said:
adelfino said:
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.
 
P

Peter Nilsson

Me said:
adelfino said:
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.]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top