Heap and Stack

D

dust

Hi,

I found a article ( http://www.maxi-pedia.com/what+is+heap+and+stack )
about heap and stack.
In this article (in sub-section "Heap and stack from programming
perspective") I see that storage for local variables for function
called by main (calcSize() in this ex.) is allocated in heap. Is it
true?. What I was assuming is space for local variables of function
called by main will be allocate in separate stack frame (within main
stack) separated by called "Activation record".
Could you please explain me this.
 
M

Mark Bluemel

dust said:
Hi,

I found a article ( http://www.maxi-pedia.com/what+is+heap+and+stack )
about heap and stack.

Well done. Have a gold star.
In this article (in sub-section "Heap and stack from programming
perspective") I see that storage for local variables for function
called by main (calcSize() in this ex.) is allocated in heap. Is it
true?.

Possibly in some instance(s)/implementation(s), perhaps not in others.
The C language has no view on the matter
What I was assuming is space for local variables of function
called by main will be allocate in separate stack frame (within main
stack) separated by called "Activation record".

Possibly in some instance(s)/implementation(s), perhaps not in others.
It's not something that the language cares about, so it's not something
that the language specifies
 
O

osmium

dust said:
I found a article ( http://www.maxi-pedia.com/what+is+heap+and+stack )
about heap and stack.
In this article (in sub-section "Heap and stack from programming
perspective") I see that storage for local variables for function
called by main (calcSize() in this ex.) is allocated in heap. Is it
true?. What I was assuming is space for local variables of function
called by main will be allocate in separate stack frame (within main
stack) separated by called "Activation record".
Could you please explain me this.

I scanned that article and do not like it one little bit. There must be
something better around, somewhere on the web.
 
T

Tom St Denis

I scanned that article and do not like it one little bit.   There must be
something better around, somewhere on the web.

It's not only a garbage presentation but it's also incorrect. Global
scope variables are never [so far as I've seen] placed on any form of
"stack". They're usually in a form of .bss segment if they're not
initialized.

I'm not going to bother reading the rest....

Tom
 
M

Malcolm McLean

In this article (in sub-section "Heap and stack from programming
perspective") I see that storage for local variables for function
called by main (calcSize() in this ex.) is allocated in heap. Is it
true?.
Normally an implementation has a stack. Each time a function is called
its parameters and local variables are pused on the stack. When the
function returns the stack is popped and the memory is reused.

However large variables, and variables whose size is not known at
compile time, are allocated from the heap, with a call to malloc().
Only a pointer to the memory area is placed on the stack. This is
because most systems work better with relatively small stacks (it's
easier to keep the stack in the cache), and because it creates
difficulties if objects are resized when on the stack. calcSize()
could well call malloc(). It's very common to do this.



 
 
S

Seebs

I found a article ( http://www.maxi-pedia.com/what+is+heap+and+stack )
about heap and stack.

It is wrong.
In this article (in sub-section "Heap and stack from programming
perspective") I see that storage for local variables for function
called by main (calcSize() in this ex.) is allocated in heap. Is it
true?.

Okay, there's two answers here.

One is: The concepts "heap" and "stack" are not necessarily part of how
a C implementation works. As long as everything works, the compiler is
free to store anything anywhere it wants.

The second is: On many common implementations, function calls use a
region of memory called the "stack" to store arguments and local variables,
while malloced memory is stored in another region called the "heap". This
is not universal, and there can be major surprises in it. The most
consistent, and significant, exception is that a lot of values are stored
in registers, and on many implementations, values in registers can be
pushed out to the stack many function calls away from where they were
local variables.
What I was assuming is space for local variables of function
called by main will be allocate in separate stack frame (within main
stack) separated by called "Activation record".

That's certainly possible, although it's not part of the formal specification.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

stack and heap 9
heap vs stack 3
Stack is slow than heap? 23
pImpl idiom: Heap vs Stack 14
Stack and Heap 16
Stack memory 3
Variables allocated on stack 44
function stack frames may overlap? 1

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top