Re: Where are the program variables stored in memory(text, data or stack segments)




"kr" <rahul.kashyap98@xxxxxxxxx> wrote in message news:69f0daad-2b9b-4753-bf3b-c40d745c7c35@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

.



Relevant Pages

  • Re: Ordering local variables
    ... Suppose it has a number of int and char automatic variables. ... Although the usual thing is for variables to be put on the stack in the ... And 2x with Sun C Compiler, once with and once without -O2. ...
    (comp.lang.c)
  • convert infix to postfix
    ... void initStack(STACK *stack); ... int precedence(char operator1, char operator2); ... void push; ...
    (comp.lang.c)
  • Re: whats wrong w/ my queries?
    ... declare @t1 table(i int, j int) ... declare @t2 table(k char, l char, m bit) ... select * from #tmp ... The man directing me has been programming for ...
    (microsoft.public.sqlserver.programming)
  • Re: Promotions while delivering parameters
    ... when we use K&R style declaration and definition of functions: ... int kandr ... the 'char' parameter p2 is promoted to 'int' while pushing into stack; ... stored on the stack. ...
    (comp.lang.c.moderated)
  • Re: Doubt on Stack variables.
    ... C does not define anything called a "stack". ... then for int then for float and after ... this only second char in the function gets memory. ...
    (comp.lang.c)