Hi,

i have some questions mostly regarding the memory segments... Can any one help me out..

1. During a function call in C, where is the return value of the called function stored.. This is mainly a question came when i was thinking of steps during a function call.
The steps i have in my mind are,
a) store the return address of calling function in stack
b) create a new stack frame for the called function. store local variables in stack
c) return to the called function after operation is finished.
d) delete stack frame and continue execution

Here in the step c, suppose i am returning a value like

return (a+b);

where both a and b are my local variables in the called function, how the caller gets this value to his function. The stack frames of both these are different.
Or
Where exactly the value of a+b is stored? In stack? If yes in which frame????

I am mainly confused about the process how the caller gets the return value..


2nd question

2. Suppose i have a global static variable and a local static variable

I am pasting the code here,

#include <stdio.h>

static int myvar= 0 ;


int main()
{


	call();	
	printf("My var in main  is %d \n",myvar);
	return 1;

}

int call()
{
	
	static int myvar = 1;
	printf("My var is %d \n",myvar);

}

just to know, how the compiler distinguishes these variables, i took objdump of these code after building it with gcc...

it showed

080495b4 l     O .bss	00000004              myvar
080495ac l     O .data	00000004              myvar.1784

here myvar is my global static and myvar.1784 is my local static.

I want to know how my local static variable is in .data segment and my global static is in .bss specifically?
and what about that peculiar .1784 naming in that local static variable...


can anybody guide me?

The questions depend on the platform. Here is a wiki article

The return value on Intel based computers is normally stored in the eax register then after the function returns the value is copied to some variable.

I want to know how my local static variable is in .data segment and my global static is in .bss specifically?
One of them ends up in .data, because it is initialized. Another one ends up in .bss because it is not (or, rather, initialized by 0).

and what about that peculiar .1784 naming in that local static variable...
This is how your compiler decided to mangle its name to take it out of the global scope.

In Intel based 32-bit programs the .data and .bss are really in the same segment. In fact, 32-bit (and 64-bit) programs do not use segments at all. Everything, including both code and data, are in the same segment because the compilers use the flat memory model. Compilers for other processors may or may not do it differently.

.data, .bss, etc have nothing to do with the architecture. They represent the structure of an object file. They are linked differently; they are stored differently, they are loaded differently.

I want to know how my local static variable is in .data segment and my global static is in .bss specifically?
One of them ends up in .data, because it is initialized. Another one ends up in .bss because it is not (or, rather, initialized by 0).

and what about that peculiar .1784 naming in that local static variable...
This is how your compiler decided to mangle its name to take it out of the global scope.

thanks.. [+1]
i got it... when i initialized my global static with value 2, it also came in .data segment. I always thought , when i implicitly initialize a global value by 0, it should go to .data. Now i know it is wrong

I understood the compiler decides the naming. Is there any specific rule for them. May be, can you point out how it is done for the compiler you are using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.