Archived

This topic is now archived and is closed to further replies.

Tree Penguin

Stack and other memory (some simple questions)

Recommended Posts

Tree Penguin    262
Hi, i got a few questions about memory: 1. How is the memory called you allocate from using new and malloc? 2. What exactly is stack memory and how much is available per program/function? 3. What are the (dis)advantages of both stack and the other memory type? Thanks in advance!

Share this post


Link to post
Share on other sites
MV    122
If my memory is good, on 68000 processors all the memory was managed through the stack.
The bottom and top of the stack are used for different purposes ; the bottom for variables, and allocations "on the fly" (new, malloc), the top can be considered as a proper stack used for the functions and subroutines.

But maybe things have changed since those times

Anyway, I dont think you should care with this. Have you found a way to use specific memory ?

Share this post


Link to post
Share on other sites
Tree Penguin    262
Actually i was just interested but i think when i said stack i meant something different. The memory i want to know the size and proper use of is the memory you use when not allocating memory with malloc, so the memory you allocate when you, for example, do this:

float largearray[200000];

Is this allocated in the stack memory or is it called different or is it (i don''t think so) allocated the same way as when using malloc or new?

Thanks in advance!

Share this post


Link to post
Share on other sites
arm    164
In c/c++ ''float array[200000];'' would typically allocate memory from the stack wheras ''float* array = new float[200000];'' would allocate from the heap.

Share this post


Link to post
Share on other sites
Ghwerig    122
1. Heap
2. Stack stores local variables and function return addresses
3. If you store a variable on the stack, this variable ceases to exist when the function returns. If you store your variable on the heap, you have to remember to free the memory

Share this post


Link to post
Share on other sites
Enigma    1410
Memory allocated with new/malloc (if you''re coding in c++ then it''s best to only use new) comes from the heap (malloc) or the freestore (new). Basically these are both large chunks of memory in the programs address space.

Stack memory is also part of the programs address space, but is basically more organised. The stack is a (conceptually at least) contiguous block of memory. At the beginning of a programs execution it starts at the beginning of the stack. When a stack variable is declared it is placed at the top of the stack and the stack pointer is moved to point to the new top. When a stack variable leaves scope and is destroyed the stack pointer is moved back down, i.e.:

at some point in program:
[0109] // garbage/unused memory
[3215] // garbage/unused memory
[0007] <- [stack pointer] // some variable
[1234] // some other variable

later on, after calling somefunc():
[0000] <- [stack pointer] // first stack variable in somefunc
[9691] // address to return to after somefunc finishes executing
[0007] // some variable
[1234] // some other variable

later on, after somefunc() returns:
[0000] // garbage/unused memory
[9691] // garbage/unused memory
[0007] <- [stack pointer] // some variable
[1234] // some other variable

Stack space is limited, so it should not be used for large objects/arrays. The advantage with stack space is that you do not have to remember to delete objects on the stack. C++ handles this automatically for you when they leave scope.
There is more heap/freestore memory available but you must remember to free/delete it when you are finished with it.

Which one to use depends on the situation:
LargeObject func1()
{
LargeObject o;
o.doStuff();
return o;
}


is probably going to be slower than:
LargeObject* func1()
{
LargeObject* o = new LargeObject();
o->doStuff();
return o;
}

because of the cost of copying the object when the function returns.

General advice is to use references to stack objects whenever possible. Dynamic sized arrays (arrays where the size is not known at compile time) can never be stack allocated, they must come from the heap/freestore.

Enigma

Share this post


Link to post
Share on other sites
Enigma    1410
Yes, the stack has a fixed maximum size but the heap/freestore are basically as big as your virtual memory space.

Enigma

Share this post


Link to post
Share on other sites
Tree Penguin    262
Thanks, i read your reply after i wrote mine but i get it now (actually it was just as i thought, but this cleared up my mind a bit ).

Does anyone know what the limit of stack memory is?
Thanks in advance!

Share this post


Link to post
Share on other sites
orbano    130
it depends on the system you are using. in windows, according to my best knowledge, there isnt (mybe somebody will kick my butt for this), but i remember when i was writing dos programmes, there was an upper limit of... lemme check my pascal... in freepascal i could set a maximum of 9999999bytes (using go32v2 extension). i think you should not be bathered about this limit (i there is any)

Share this post


Link to post
Share on other sites