Is this the same?

S

S.Tobias

Wat said:
char a[100] = "";
the same as,
char a[100];
a[0] = '\0';


If `a' is a static object - yes (sort of, static objects are automatically
initialized to zero).

If `a' is automatic object - no. In the initialization if some
member(s) is(are) initialized, all the remaining ones are initialized
to zero. If none is explicitly initialized - the whole object remains
uninitialized.
 
S

Stephen Mayes

S.Tobias said:
Wat said:
char a[100] = "";
the same as,
char a[100];
a[0] = '\0';


If `a' is a static object - yes (sort of, static objects are automatically
initialized to zero).

Is this mandated by the standard?
I have a free compiler ( I won't name ) that creates PE format executables.
When I don't initialize a static array, it lists the data in a .bss header,
so I have always assumed that the data was uninitialized.
However, I just compiled the following and it DOES output all zeros. ( And,
as you said, when the array is declared inside main, it doesn't. )

#include <stdio.h>

unsigned char s[20];

int main (void)
{
int i;

for (i = 0; i < 20; i++)
printf ("0x%02X\n", s);

return 0;
}
 
A

Andrey Tarasevich

Wat said:
Is

char a[100] = "";

the same as,

char a[100];
a[0] = '\0';

?

No. The former initializes the whole array with zeroes. The latter sets
the first element to zero, but the rest remains unchanged.
 
J

Jens.Toerring

Stephen Mayes said:
S.Tobias said:
Wat said:
char a[100] = "";
the same as,
char a[100];
a[0] = '\0';


If `a' is a static object - yes (sort of, static objects are automatically
initialized to zero).
Is this mandated by the standard?

Yes, in the section "Initialization" of C89 you will find:

If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant.

and for C99 (last draft) you have

If an object that has static storage duration is not initialized explicitly,
then
- if it has pointer type, it is initialized to a null pointer
- if it has arithmetic type, it is initialized to (positive or unsigned) zero
- if it is an aggregate, every member is initialized (recursively) according
to these rules
- if it is a union, the first named member is initialized (recursively)
according to these rules.

Regards, Jens
 
M

Michael Mair

Stephen said:
char a[100] = "";
the same as,
char a[100];
a[0] = '\0';


If `a' is a static object - yes (sort of, static objects are automatically
initialized to zero).


Is this mandated by the standard?

Yes. Whenever you declare an object with static storage duration
and do _not_ initialize it, it is initialized to zero (that is,
to whatever is zero in the respective context).


Cheers
Michael
 
X

Xenos

Andrey Tarasevich said:
Wat said:
Is

char a[100] = "";

the same as,

char a[100];
a[0] = '\0';

?

No. The former initializes the whole array with zeroes. The latter sets
the first element to zero, but the rest remains unchanged.

--
That completely depends on whether they are static or not.

DrX
 
C

Chris Croughton

Is this mandated by the standard?
Yes.

I have a free compiler ( I won't name ) that creates PE format executables.
When I don't initialize a static array, it lists the data in a .bss header,
so I have always assumed that the data was uninitialized.

BSS is a term from some early systems where it stood for "Blank Storage
Section" (or something similar). It was a section in a load image which
was (and still is) initialisd to zero at load time, but didn't take up
lots of space in the image (instead of making an image file which
consisted of thousands of zero bytes it just said "reserve this much
memory and set it to zero before running the program").

For this reason some older programmers will say that it is 'better' to
not explicitly initialise variables at file scope to zero, because:

int a[1024] = {0};

created an image file with sizeof(int) Kbytes of zeros in it whereas

int a[1024];

didn't increase the size of the image file. I suspect that some
compilers will optimise the former to the latter now, if it matters.

Where it does often matter still is with embedded systems, where the
various link sections can be put into different types of memory (code
and constant data into ROM, initialised data into ROM which is then
copied to RAM, BSS just in RAM and set to zero by the start code, etc.)

Chris C
 
A

Andrey Tarasevich

Xenos said:
Wat said:
Is

char a[100] = "";

the same as,

char a[100];
a[0] = '\0';

?

No. The former initializes the whole array with zeroes. The latter sets
the first element to zero, but the rest remains unchanged.

--
That completely depends on whether they are static or not.
...

Strictly speaking, what I said is true regardless of storage duration of
the object. I intentionally formulated the description for the latter
case the way I did: "... the rest remains unchanged".
 
C

Chris Torek

BSS is a term from some early systems where it stood for "Blank Storage
Section" (or something similar).

Historical note: the name BSS comes from an IBM assembler, where it
stands for "Block Started by Symbol". The same assembler also had
a BES directive for "Block Ended by Symbol". (The Fortran compiler
used BES to create arrays, as the hardware had a peculiar manner of
doing array indexing in which A(1) had the highest address, A(2) had
a next-lower address, and so on -- the index was treated as if it
were negative.)
Where it does often matter still is with embedded systems, where the
various link sections can be put into different types of memory (code
and constant data into ROM, initialised data into ROM which is then
copied to RAM, BSS just in RAM and set to zero by the start code, etc.)

Modern linkers are even fancier, and can put specific (named)
sections in specific orders and regions and so on. Of course,
actually *using* any of this requires going beyond Standard C --
and one then discovers that each different linker for each system
does something slightly different. So if you can accomplish what
you need to do in "pure" Standard C, it saves a lot of effort. :)
 
C

Chris Croughton

Historical note: the name BSS comes from an IBM assembler, where it
stands for "Block Started by Symbol". The same assembler also had
a BES directive for "Block Ended by Symbol". (The Fortran compiler
used BES to create arrays, as the hardware had a peculiar manner of
doing array indexing in which A(1) had the highest address, A(2) had
a next-lower address, and so on -- the index was treated as if it
were negative.)

Thanks, when I first came across it the documentation called it Blank
Storage Section, I thought it had been called something else but
couldn't remember what.
Modern linkers are even fancier, and can put specific (named)
sections in specific orders and regions and so on. Of course,
actually *using* any of this requires going beyond Standard C --
and one then discovers that each different linker for each system
does something slightly different. So if you can accomplish what
you need to do in "pure" Standard C, it saves a lot of effort. :)

Actually, linkers have been doing 'fancy' things like that for at least
20 years (I wrote one around that time for an embedded system and the
idea was already established in at least one commercial linker). It is,
of course, beyond the scope of the language (indeed, it is unrelated to
any specific language) and is mainly of use in embedded systems with
specific hardware addresses and ranges.

Chris C
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top