The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



Thread: Heap and stack
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 04-28-2008
vino's Avatar
vino vino is offline
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Quote:
Originally Posted by andryk View Post
Hi,
It'll be on stack i suppose and in general all variables declared inside a function will be on stack as well.
Code:
unsigned long sp(void) {
   asm("movl %esp, %eax");
}
void main() {
  int *a;

  a=malloc(16*sizeof(int));
  printf ("addr of a is %p, sp is %p and malloc is %p\n",&a,sp(),a);
}
In the above case, a will point to the memory in the heap storage. Heap because of the malloc.

On the other hand, in the following case, a will point to a memory location in the stack.
Code:
int i = 10;
int *a = &i;
Reply With Quote