The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
what is stack winding and stack unwinding amitpansuria High Level Programming 2 08-07-2007 05:46 PM
AIX 5.3 Heap Memory with SAP johnf AIX 3 10-23-2006 05:21 AM
monitor jvm heap size rein UNIX for Advanced & Expert Users 0 07-21-2005 08:29 AM
heap size for JVM! i2admin Filesystems, Disks and Memory 1 05-09-2002 09:09 AM
heap size! i2admin Filesystems, Disks and Memory 2 05-08-2002 10:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2008
naan naan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 31
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 (permalink)  
Old 04-28-2008
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
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 !

Last edited by andryk; 04-28-2008 at 02:44 AM..
  #3 (permalink)  
Old 04-28-2008
vino's Avatar
vino vino is online now Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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;
  #4 (permalink)  
Old 04-28-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 04-30-2008
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
Quote:
Originally Posted by jim mcnamara View Post
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 ...
  #6 (permalink)  
Old 04-28-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0