Link to home
Start Free TrialLog in
Avatar of kambrish
kambrish

asked on

Static variable in memory

HI,
 I read that static variables store in memory in two parts. If static variables are initialized, as in
 static int i=10
 these are stored in initialized data area whereas if its given as
 static int i;
 it is stored in uninitialized data area.
 In both the cases, the value of i is initialized to a value (in 1st case 10 and in 2nd case 0) then why these are stored in two different parts of memory. These should be stored in initialized part of memory??
 Rgds Ambrish

 
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

In general (though this willl vary between OSes and compilers),
the static variables are stored in a region of memory containing global variables which exists from when
the program is loaded until it is unloaded.

Now, the static keyword has a few different uses, so the context is very important:
1. Within a function, declaring a variable as being static causes it to exist between calls to that function. From "The Annotated ANSI C Standard" (page 58):

When applied to a local variable, it causes storage for the variable to remain in existence the entire duration of the program's execution. Typically, local variables are stored on the stack, but a static local variable is not. It is usually stored in the same region of memory used to hold global variables. In essence, a static local variable is a global variable with its scope restricted to a single function.

ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kambrish
kambrish

ASKER

Ankur,
 dont u think that although the variable is not initialized by us (say x and z in the example u mentioned) but still it is initialized to a value 0 by default. Still, compiler keeps in .bss section? Is .bss section an uninitialized data area as I am not sure of this? If yes, it means that compiler takes it as an uninitialized value. Isn't it?
 
 
Hi Kambrish,

the reason for allocating two different memory address for initialised and un-initialised variables is, reduce the size of your execuable file.
when you define a variable as below

static int a[100];
then the compiler will not allocate memory till a.out file is loaded. but it keeps the information that, in future it will reqire 100 integer. and it is compiler design that the bss segment is set to 0.
Hi,

The bss section is used for local common variable storage. You may allocate address space in the bss section,
but you may not dictate data to load into it before your program executes. When your program starts running,
all the contents of the bss section are zeroed bytes.

Therefore,all the data in the bss section is initialized to 0.

HTH.


Hi ankuratvb,
I wonder, though: Is it possible to have relocation entries for BSS symbols? Won't make too much sense unless you want to have pointers to other symbols.

If I remember correctly, the address entry to be relocated in the final ELF binary doesn't contain a section offset anymore, like it was in a.out times.

Cheers,

Stefan
Hi stefan,

There is a special section  .rel.bss
which holds relocation information.These relocations are applied to the .bss section.

since the bss section contains data still  to be relocated,there has to be some relocation
information as well.
ankuratvb,
Do you know by chance how to get rid of an ELF symbol table? strip doesn't remove everything, and I don't care if I can't use dlsym/dladdr anymore.
Hi stefan,

Have u tried running strip using some of the options that remove additional parts from the
symbol table which the default call does not?

http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?strip

This is the man page for strip.See if this is what u need.