stack vs. heap memory

Started by
2 comments, last by simeygrimey 20 years, 8 months ago
Which one is better and for what reason? Like which one should you use in which situations? Thanks
Advertisement
Local variables are allocated on the stack. Dynamically allocated ones reside on the heap. You don''t have much choice about it. As for which you use when: Use local variables for everything. When that''s impossible, use the heap.
The stack is used by your program for passing arguments, remembering where to return after a function returns, and holding local variables. It is also limited in size, so try to avoid putting anything big on it, like an array for example, especially if your program has recursive functions.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
To add another descriptor to those given so far: Use the heap for all objects whose lifetimes extend past the return from the function call during which they were allocated.

How appropriate. You fight like a cow.

This topic is closed to new replies.

Advertisement