stack vs. heap memory
Which one is better and for what reason? Like which one should you use in which situations? Thanks
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.
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.
How appropriate. You fight like a cow.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement