dynamic memory

This is a discussion on dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Is dynamic memory only used for arrays? I understand that if you don't have a fixed size for your array, ...

Yes! If the memory you're trying to use is large (or a lot of it), then you use dynamic memory since the stack is limited to about 1 MB (or less on embedded systems), but the heap is limited to 2 GB (on Windows; may vary on other platforms, max 4 GB for 32-bit systems).

Yes! If the memory you're trying to use is large (or a lot of it), then you use dynamic memory since the stack is limited to about 1 MB (or less on embedded systems), but the heap is limited to 2 GB (on Windows; may vary on other platforms, max 4 GB for 32-bit systems).

So, is that a yes, it is only used for arrays or yes, it can also be used in variables?
Thanks, Elysia..

As I explained, if the memory you need is very big, then you use dynamic memory. Dynamic memory is not limited to arrays, it can be used for any type of variables.
Dynamic memory is also used when you need to create memory that outlives the length of a function or when you dynamically need to create a variable or object.

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Uhh no, profilers don't catch memory leaks (well, some might, but that is not the purpose of a profiler). They catch parts that needs to be optimized.
Dynamic memory isn't hard at all. You just need smart pointers. Classes that delete memory when no longer in use. C++ is so powerful, yet so underutilized by many.

You might also use dynamic memory when the lifetime of the variable you are creating needs to be longer than the function you create it in. One example is a Factory function that creates a variable of the correct kind of type depending on some input it gets. The function cannot create a local variable because that will die when the function ends, so it will create an instance using dynamic memory that will continue to exist when the function ends.