This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: .bss section and uninitialised variables


In C/C++/most languages, you should not make any assumption about the
initial value of variables - it is undefined by the language, and usually
undefined on the platform as well. Java is an exception here by defining
the matter. If you want it initialized, give it an initializer :-)

Side note - some C++ compilers do initialize it (when in debug mode), but
with a garbage value to help catch such problems (since the OS is likely to
give you zero-initialized memory *most* of the time). I don't think g++
does this, but I've never really looked - on linux I have valgrind, which
pretty much obviates any need for half-measures :-)

Gisle Grimen wrote:

> hello,
> 
> I am not an expert on linux or gcc, but i was wondering about one thing
> with variables and .bss section.
> 
> You have an relocatable elf file(.o) generated with gcc (3.3.5). My
> question is why the variable defined myvar (se simple src below) is not
> included in the .bss section so that it will be initialized to 0;
> 
> What happens is that i find a relocation entry and a symboltable entry for
> the variable, but it is not defined a section to which it belongs. If you
> initialize the variable with 0 (int myvar=0;) then everything is ok and it
> get put in the .bss section.
> 
> As i said i am not an expert in gcc and c, but does c allow such
> "undefined" variables? Java which i have much more experience with i know
> would initialise myvar with a 0 for me.
> 
> #src
> # include <stdio.h>
> int myvar;
> 
> int main(void){
>  printf("hello");
> }
> 
> 
> -Gisle



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]