Is my global part of BSS ?

F

Francis Moreau

Hello,

I declared the following global variable:

struct {
int a,
int b,
} my_global = {
.a = 5,
};

So only one field of the structure is initialized, so I expect that
the variable is part of .data section with the 'b' field init to 0.

But if I look where the compiler stores the global, it's in bss...

Could anybody enlight me ?

Thanks
 
A

Antoninus Twink

So only one field of the structure is initialized, so I expect that
the variable is part of .data section with the 'b' field init to 0.

But if I look where the compiler stores the global, it's in bss...

Which compiler are you using? I have gcc 4.3.2 to hand, which puts the
struct in the .data section as expected (with the .b field initialize to
0). I can't why it would get put in the .bss section unless you're
passing a strange flag to the linker.
 
K

Kaz Kylheku

Hello,

I declared the following global variable:

struct {
int a,
^ ooops, syntax error. :)
int b,
} my_global = {
.a = 5,
};

So only one field of the structure is initialized, so I expect that
the variable is part of .data section with the 'b' field init to 0.

But if I look where the compiler stores the global, it's in bss...

Why are you looking at that at all? Is it because the actual behavior of
the running program is wrong? Are you investigating the bug that a is not
getting set to 5?

The structure is one object, so it has to live in one section or another. If
it's in a section dedicated to objects that are initialized to all-zero-bit
values simply by virtue of being in that section, then additional steps have to
be taken to make sure that the a member is set to 5 prior to program startup.
If that is not done, this is a bug.
Could anybody enlight me ?

I tried it with gcc 3.4.6 and 4.3.2. Both put my_global into .bss if
I changed the designated initializer to .a = 0, and into the .data
section if I left it as .a = 5.

What compiler are you using?

Also, do you have the syntax error in your test program? Maybe you're
not noticing the compiler diagnostic, and then assuming that a stale
output file is the output.
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top