It's quite simple why you would not use stack-only or heap-only.
1. Heap-only is not possible, you need to store AT THE VERY LEAST a single pointer on the stack.
2. Stack-only will only work for very small programs (who require a FIXED amount of memory and whose memory request is under a certain limit, usually 64KB minus the memory used by the callstack, which shouldn't be above 1KB).
So, if you have 20GB of RAM, you would be using heap memory.
Also:
- Always free (delete) your memory.
Even if you do have 20GB of RAM (make your lie worth it and take us a picture, it's not something I see everyday), not everyone else does.
My sister still runs on a 256mb machine using Windows XP SP2.
If you were to leak memory, her PC would likely block.
So please be respectful to your sisters.
- Leaking memory is lying to your operating system (And if you hate your operating system, change it, don't leak memory).
Your operating system always waits you to "physically" release the memory once you're done with it, so it can use that memory for something else.
- Memory is something important, and is easy to waste.
You'll learn it quite fast, memory must not be wasted for useless things.
Always try to cache what you can, if you cache your things in the optimal way you will see the program running very good.
It will "run faster", but make sure to free what you don't use in the cache (eventually put a timer running in a thread, checking for the last access of the cache and deleting that part of the cache which hasn't been used in the last two minutes).
[Edit]
- Leaking can result in more leaking
1 2 3 4
|
{
std::vector<int> * myVector = new std::vector<int>;
myVector->resize(5000);
}
|
What happens here?
You're not just leaking
sizeof(std::vector<int>)
bytes.
You're also leaking
sizeof(int)*5000
(or more, depending on implementation) bytes.
Now, for that picture :D