lolu
lolu

Reputation: 370

BSS segment in c, way of progress

I'm new in c programing and im trying to better understand where variables are saved in regarding to the memory layout of a C program.

i got the following code:

int addr5;              
int addr6;

/*code continues*/

int main (int argc, char** argv){

    printf("- &addr5: %p\n",&addr5);        
    printf("- &addr6: %p\n",&addr6);

/*code continues*/

now, when i run this code, i get that:

now, from what i have read, i got that the heap memory starts from lower values (and grows as we go on) and that the stack memory starts from high values (and dec as we go on).

since i saved addr5 and addr6 as uninitialized var, and outside of a function scoop, aren't they supposed to be saved in the BSS segment? and if so, isnt bss segment grows as we go, since its in the heap?

shouldn't addr6 be bigger than addrs5 as it was initialized later?

thank you.

Upvotes: 2

Views: 434

Answers (1)

juhist
juhist

Reputation: 4314

No, the .bss segment is not in the heap. The .bss segment and the .data segment are fixed-size segments that are typically close to the heap in address space, but they are clearly distinct from the heap.

No, the .bss segment does not grow. The number of global variables you define in your program stays constant during the program execution. If you load a dynamic library, then that dynamic library will have its own .bss segment that is placed to another location in the address space.

Upvotes: 2

Related Questions