By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,812 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,812 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 20 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>
Nov 20 '07 #2

P: n/a
In article
<69**********************************@e23g2000prf. googlegroups.com>, kr
<ra*************@gmail.comwrote on Tuesday 20 Nov 2007 8:17 pm:
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 <news:comp.os.ms-windows.programmer.win32or
<news:comp.unix.programmerfor an UNIX system and so on.

Nov 20 '07 #3

P: n/a
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?

--
Er*********@sun.com
Nov 20 '07 #4

P: n/a

"kr" <ra*************@gmail.comwrote in message
news:69**********************************@e23g2000 prf.googlegroups.com...
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 20 '07 #5

P: n/a
In article <1195574401.179806@news1nwk>,
Eric Sosman <Er*********@Sun.COMwrote:
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '07 #6

P: n/a
Malcolm McLean wrote:
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?
Nov 21 '07 #7

P: n/a
Richard Tobin wrote:
In article <1195574401.179806@news1nwk>,
Eric Sosman <Er*********@Sun.COMwrote:
> 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.
Ha! Well said!

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.