Advertisement

Warning to all Win32 programmers!!!

Started by August 23, 2000 04:00 PM
12 comments, last by baskuenen 24 years, 4 months ago
A stack is limited in space and it grows downward so when it hits the bottom there''s no more space left.

A heap is a mechanism which allocates/deallocates some space in memory(I think could be paged out to harddrive also???) based on demand for that memory. I think it''s unlimited. The arguments you give to your function and everything inside the body of that function gets placed on the stack unless you new something which in that case you get a pointer to heap and so must call delete to free that space from the heap.

Using recursive function calls will place arguments on the stack so stack can blow up if you don''t provide a sentinel (i.e. stopping value) in your recursive function. You can use static keyword inside your function body to tell the compiler to place that value in the global namespace and remember what it contains the next time you enter that function again. You write it as: static int value = 5; then you increment value, exit the function, enter the function again and value will be 6. When your function exits everything in the body including pointers gets destroyed. So if you create a pointer in function body and you new something then you either delete it in the function or pass the pointer as a return value so you wouldn''t lose track of the allocated memory.

Basically, use stack for small data and heap to hold large blocks of data that you like to keep around for a while. You can also do some funky programming by creating your own heap space using new to allocate fixed block of memory and then create a mechanism that will emulate new/delete functions. I think this was used by developers to manage texture space in software based 3D engines if I''m not mistaken.

my homepage
E-Mail: BlueOrbSoftware@mailcity.com
baskuenen:

Actually, 1 MB is the limit, but you start off with about 4 KB. The stack grows in 4 KB increments (or however much you suddenly need) according to how much you use. I suppose that the size of the stack doesn''t shrink (for performance reasons) however it probably keeps track of the unused space and reuses it when you allocate more variables on the stack.


JD:

As such, it would seem to be the perfect tool for a fixed block of memory like you described about that 3D engine. You can set the stack if you really need to (inside your compiler''s options), and it would sure beat writing those memory management functions yourself. Most programs shouldn''t require an adjusted stack size, and should simply use new/delete as they are already written.


- null_pointer
Sabre Multimedia
Advertisement
Don''t stack and heap start at opposite ends of memory allocated for you program, and grow towards eachother? I''m pretty sure that''s how Unix does it, and DOS/Win as well, but i''m not sure.
If you use a lot of stack, the memory available to the heap gets smaller. I know this memory model is used, I''m just not sure if it is in this case.
quote:
Don''t stack and heap start at opposite ends of memory allocated for you program, and grow towards eachother?


I don''t know, but when I was programming in 16-bit DOS, I think the segment registers (CS,ES,DS) would point at the beginning of the data segment. The stack segment register would point at the end of the data segment, I think.

null_pointer''s
quote:
You can set the stack if you really need to (inside your compiler''s options), and it would sure beat writing those memory management functions yourself.


If you''re using VC++ 6 you can set the stack size in project settings/link/output/stack allocations. It''s 1 megabyte in size by default.

I haven''t written a heap manager myself so I don''t know what the benefits are over the compiler''s heap manager and thus I will leave this topic for someone in the know


my homepage
E-Mail: BlueOrbSoftware@mailcity.com

This topic is closed to new replies.

Advertisement