Rajesh Pal
Rajesh Pal

Reputation: 128

Where memory will be allocated to "Uninitialized Static variable" upon initialization?

Uninitialized static variable are always allocated in BSS. While .bss section is static as memory is allocated at compile time. As per many books "only variables that are initialized to a nonzero value occupy space" in executable. After program is loaded into memory, uninitialized static variables are still .bss.

**What happens when a function initializes it? ** Will it get moved to some other area?

Upvotes: 3

Views: 984

Answers (3)

Rajesh Pal
Rajesh Pal

Reputation: 128

Upon initialization, memory is allocated to "Uninitialized Static variable” and this is moved to .data section.

Code File:

int a,b,c;

int main()
{

a=1;
b=2;
c=3;

scanf("%d",a);
}

Execution:

 # size a.out   
 text      data     bss     dec     hex filename
 1318       284      16    1618     652 a.out

# size core.18521 
text      data      bss     dec     hex   filename
28672    180224       0  208896   33000   core.18521 (core file invoked as ./a.out)

Upvotes: 2

user3629249
user3629249

Reputation: 16540

the rest of the quote:

"In the executable file, only variables that are initialized to a nonzero value occupy space."

I.E. when the executable file is loaded into memory, the needed space is allocated

Upvotes: 2

Dmitry Poroh
Dmitry Poroh

Reputation: 3825

.bss doesn't occupy space in executable file. When program is started .bss is allocated and filled with 0. All not initialised object are located there. So when you initialise that variables memory is allocated.

Upvotes: 1

Related Questions