Re: Where are the program variables stored in memory(text, data or stack segments)
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Tue, 20 Nov 2007 22:26:57 -0000
"kr" <rahul.kashyap98@xxxxxxxxx> wrote in message news:69f0daad-2b9b-4753-bf3b-c40d745c7c35@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi All,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.
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?
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
.
- Follow-Ups:
- References:
- Prev by Date: Re: "Criticism of the C programming language ??????"
- Next by Date: Re: Some more questions about C
- Previous by thread: Re: Where are the program variables stored in memory(text, data or stack segments)
- Next by thread: Re: Where are the program variables stored in memory(text, data or stack segments)
- Index(es):
Relevant Pages
|