Heap and stack


 
Thread Tools Search this Thread
Top Forums Programming Heap and stack
# 1  
Old 04-28-2008
Heap and stack

Hi,

I have a basic doubt here.

Consider the following code snippet:

main()
{
int *a;
.
.
}

Here the memory for a gets allocated in heap or stack.
# 2  
Old 04-28-2008
Hi,
It'll be on stack i suppose and in general all variables declared inside a function will be on stack as well.
You can print the address of where a resides and print the value of stack pointer (SP) you then should have the same base address ...
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);
}

Of course i assume you're on intel-based Smilie!

Last edited by andryk; 04-28-2008 at 03:44 AM..
# 3  
Old 04-28-2008
Quote:
Originally Posted by andryk
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;

# 4  
Old 04-28-2008
Code:
main()
{
int *a;
.
.
}

a, itself, is on the stack. What part of memory a "is aimed at" is indeterminate in your code. It could be anywhere. Show us something like a=&some_int and then we can tell you. As it is there is no way to answer.

Next problem - gets works on string pointers, not int *
So I have no idea where gets would be working.

If you are worried about heap being "slower" than stack, don't. Unless you can definitely show that a gets call is a bottleneck - by using a profiler - it is a waste of programmer time to fuss over stuff like that.

gets works against the actual memory referenced by the pointer, it has nothing to do with where the pointer itself lives.

Andryk - void main () is a BAD idea, especially in a UNIX forum.
# 5  
Old 04-28-2008
Memory for both heap and stack is allocated dynamically. The difference is that while stack memory is allocated automatically by the kernel whenever a function is called...heap memory is allocated only-on-request when the program calls malloc(). But as jim mcnamara has pointed out as a programmer you should not fuss over it.
# 6  
Old 04-30-2008
Quote:
Originally Posted by jim mcnamara
Code:
main()
{
int *a;
.
.
}

a, itself, is on the stack. What part of memory a "is aimed at" is indeterminate in your code. It could be anywhere. Show us something like a=&some_int and then we can tell you. As it is there is no way to answer.

Next problem - gets works on string pointers, not int *
So I have no idea where gets would be working.

If you are worried about heap being "slower" than stack, don't. Unless you can definitely show that a gets call is a bottleneck - by using a profiler - it is a waste of programmer time to fuss over stuff like that.

gets works against the actual memory referenced by the pointer, it has nothing to do with where the pointer itself lives.

Andryk - void main () is a BAD idea, especially in a UNIX forum.
LOL, okay sorry for that, i was just 'quick-coding' to show the diff between vars allocated on stack and on heap ... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Kernel Stack vs User Mode Stack

Hi, I am new to the linux kernel development area. I want to know what is the difference between kernel mode stack and user mode stack? Does each process has a user mode stack and a kernel mode stack?? Or Each process has a user mode stack and there is only one kernel mode stack that is shared by... (4 Replies)
Discussion started by: saurabhkoar
4 Replies

2. Programming

find size of heap allocated

I want to find the size of the total memory allocated on the heap for the following statement: int* a = new int;How can I use the sizeof operator for this? I used: printf("\t===> %d\n",sizeof(*a)); Is this statement correct? I have asked the question because when I checked the memory of... (13 Replies)
Discussion started by: rupeshkp728
13 Replies

3. HP-UX

Heap fragmentation on HPUX

Hi All, We are facing issues on HPUX with the C heap region growing. We use a product for CRM by name ClarifyCRM and it uses a native layer for DB access. so there are best practices in place to actual control memory. recently we have seen issues that the C heap region is growing faster than... (0 Replies)
Discussion started by: ramchand75
0 Replies

4. HP-UX

Heap fragementation on HPUX

The Resident size(as observed from top) of my process is increasing. But, the behaviour is very random. My process works on request reponse model. So when i put some request load on my process the memory starts increasing. For initial few hours (approx ~3 hrs) it increase at a rapid rate and after... (1 Reply)
Discussion started by: atgoel
1 Replies

5. Programming

what is stack winding and stack unwinding

helo can u tell me what do you mean by stack winding and stack unwinding Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

6. AIX

AIX 5.3 Heap Memory with SAP

Any idea whether this parameter can be set / adjusted and where please? (3 Replies)
Discussion started by: johnf
3 Replies

7. Filesystems, Disks and Memory

heap size for JVM!

Hi all, Thanks 'thehoghunter' and 'hugo' for the comments! I've to increase the size of the heap size for AIX 4.3.3. Now what's the command that I've and also is it something similar to growing the file system in Solaris (growfs) (1 Reply)
Discussion started by: i2admin
1 Replies

8. Filesystems, Disks and Memory

heap size!

I'm a new guy to this field and I'm learning a lot about UNIX! Can any explan to me what exactly does 'heap size' mean and how can i increase the size for AIX 4.3.3? (2 Replies)
Discussion started by: i2admin
2 Replies
Login or Register to Ask a Question