Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Memory Segments Queries (http://www.go4expert.com/forums/memory-segments-queries-t14987/)

kanaks_go4ex 6Nov2008 00:59

Memory Segments Queries
 
Analyzing the memory layout for my sample program in Linux Environment.

----------------------------------

#include<stdio.h>

int test_g = 1; // Data or BSS ? Data section Right ?
int test_h; // Data or BSS ? BSS section Right ?

static int test_s = 1;
static int test_q;


int func1(bool a ,bool b)
{
short temp;
static int var_temp; // Stack or BSS ?
return 0;

}

int main(void)
{
int m_var = 0; // Will be assigned in Stack section Right ?
static int main_temp = 0 ; Which section Data or BSS ?
m_var = func(true,true);// Will be assigned in Stack section Right ?

return m_var;

}


help me for the queries added as comments in 'C' Program.
------------------------------------


My Qs Is :

What are all the sections shared during ,when the function call func1 made from the main?

Generally every function call has its own stack.Other sections bss , data . text are shared between the multiple process or threads ?.
will the heap shared ?



Will the compiler assign the sections during the program compilation itself?












please help me on this.

xpi0t0s 6Nov2008 02:44

Re: Memory Segments Queries
 
Please don't post duplicate threads. http://www.go4expert.com/showthread.php?t=14988

What you could do is to compile to assembly then you can look this stuff up in the generated code.

xpi0t0s 6Nov2008 02:56

Re: Memory Segments Queries
 
> Will be assigned in Stack section Right ?

Doesn't make sense. Assignments don't happen in specific sections; the result is assigned to the variable wherever it is. If you assign to something on the stack, you could argue the assignment is done "in the stack section", but that doesn't really make a lot of sense.

> Generally every function call has its own stack.

Not true. Functions usually share the same stack, and as you nest deeper and deeper you will go further and further up the stack, which is why you can blow the stack if you nest too deeply.

> Other sections bss , data . text are shared between the multiple process or threads ?

Sharing depends largely on how the OS works. Generally threads refer to lightweight processes that share the same data, whereas processes have their own address space.

> Will the compiler assign the sections during the program compilation itself?

Yes, the compiler handles all this for you.

BSS is static data, which is not the same as variables initialised with the static keyword. For example static char *foo="Hello"; foo is in the data section, "Hello" is in BSS and in this case static means it can't be modified. char bar[32]; strcpy(bar,foo); - bar is on the stack. In strcpy, pointers foo and bar are on the stack as strcpy's arguments, and bar points further down the stack (or up, depending which way you look. Stacks usually grow downwards from high memory, although conceptually they grow upwards. If you push something on the stack at 1000, the next push (assuming 1-byte pushes) will be at 999. So it grows upwards conceptually, but downwards in memory).


All times are GMT +5.5. The time now is 06:23.