[C++] Dynamic memory question.

Samael88

Evil always finds a way
Reaction score
181
Hi,

This probably goes for other languages as well, but I am wondering what kind of pros and cons you get from using the dynamic memory features of c++?

As I understand it is the "new" and "delete" commands and probably a little more, please correct me if I am wrong.

As I see it, if I use dynamic memory the memory usage of the program will be pending a lot more than if I don't right?

I am asking because last time I tried using it I killed one of my ram sticks somehow, it got probably got really overloaded.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
Pros:
* Possible to use a variable amount of memory (not variable as in "different over time" but as in "allocated depending on run time values"), though you can do this on the stack too but I don't think there's a C/C++ way to do it.
* Memory allocation no longer restricted to the scope of a function call
* Much more memory available to you (heap is usually a lot larger than the stack, though there might be compiler flags to change that)

Cons:
* You have to manage the memory manually (or use a garbage collector library)
* Memory allocations are slower than memory allocated on the stack, sometimes so much slower that it actually matters.
* Some allocation patterns fragments the heap, leading to slower allocations and memory waste. Another allocation strategy might help here.

Though, you usually don't have a choice between the stack and the heap. When you need large pieces of memory, dynamic memory size or memory that lives longer than the current function scope use the heap otherwise you use the stack. As a rule of thumb...
 

Samael88

Evil always finds a way
Reaction score
181
Thank you very much phyrex1an, that answered my question really well:thup:
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top