Where are the program variables stored in memory(text, data or stacksegments)

K

kr

Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}


char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?

Thanks and Regards,
kr.
 
J

Johannes Bauer

kr schrieb:
[Snip]
This program contains several variables (global, local and dynamically
allocated variables).

I could tell you, but it looks far too much like a homework question.
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?

Try nm.

Greetings,
Johannes
 
M

Mark Bluemel

kr said:
Hi All,
Suppose I consider a sample program as given below:- ....

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap?

Read the reference documents for your platform and compiler. The C
language has no concept of what you are referring to as segments, so any
answer has to be in terms of the platform (hardware, operating systems)
and compiler, as far as I can see.
 
S

santosh

kr said:
Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}


char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?

Standard C does not define segments or stack or heap, though they may be
ubiquitous.

Some architectures need a stack to be simulated on a heap while others
may lack the heap altogether. There are also executable formats which
define few or no segments at all, while the meaning and use of any
segments may vary subtly depending on the operating and the compiler in
question.

It is best to ask such platform specific questions in a group for your
platform or compiler. For example, if you are using Windows you might
want to try <or
<for an UNIX system and so on.
 
E

Eric Sosman

kr wrote On 11/20/07 09:47,:
Hi All,
Suppose I consider a sample program as given below:-
[...]

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?

Others have mentioned that the C language has no notion
of "segment," although some specific implementations might --
so your question isn't about C, but about Megahard C or
DrillAir C or LikeWater C or whatever implementations you
care about, and the answers may be different for each.

Another point I haven't seen mentioned yet: What about
variables that are stored in CPU registers, or variables
that sometimes reside in registers and sometimes in memory,
or variables that are optimized out of existence?
 
M

Malcolm McLean

kr said:
Hi All,
Suppose I consider a sample program as given below:-

#include<stdio.h>
#include<stdlib.h>
int i;
int main()
{
char *test(int i);
char *tmp = NULL;
i = 10;
tmp = test(i);
printf("%s\n", test(i));
free(tmp);
return 0;
}


char *test(int i)
{
char *str = (char*)malloc(20);
sprintf(str, "i value is: %d", i);
return str;
}

This program contains several variables (global, local and dynamically
allocated variables).
How to figure out which variable is stored in which segment viz. text,
data, stack or heap? Is there any tool to get an idea about it?
local variables go on the stack, malloc() allocates memory from the heap,
and global variables go in a special area set up on program start up.

The exact details vary from system to system. For instance sometimes the
stack will grow upwards, sometimes downwards. Often constant global data is
segregated from variables.

Note also that this scheme isn't used by absolutely every C compiler, though
it will almost certainly be the way your system works.
 
R

Richard Tobin

Another point I haven't seen mentioned yet: What about [...]
variables that are optimized out of existence?

They're stored on the heap. Very efficiently.

-- Richard
 
M

Mark McIntyre

Malcolm said:
local variables go on the stack, malloc() allocates memory from the
heap, and global variables go in a special area set up on program start up.

IMHO its dangerous to start your post with such a definitive statement,
even though you acknowledge it to be wrong later on. Unless you were
being ironic?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top