Skip to main content

What is the point of dynamic allocation in C++? [Resolved]

I really have never understood it at all. I can do it, but I just don't get why I would want to.

For instance, I was programming a game yesterday, and I set up an array of pointers for dynamically allocated enemies in the game, then passed it to a function which updates their positions.

When I ran the game, I got one of those nondescript assertion errors, something about a memory block not existing, I don't know. It was a run-time error, so it didn't say where the problem was. So I just said screw it and rewrote it with static instantiation, i.e.:

Enemy tempEnemy = Enemy(3, 4);

And it immediately worked perfectly.

Now sure, some of you may be thinking something to the effect of "Maybe if you knew what you were doing," or perhaps "n00b can't use pointers L0L," but frankly, you really can't deny that they make things way overcomplicated, hence most modern languages have done away with them entirely.

But please-- someone -- What IS the point of dynamic allocation? What advantage does it afford? Why would I ever not do what I just did in the above example?

Question Credit: Aerovistae
Question Reference
Asked March 10, 2018
Tags: , pointers
Posted Under: Programming
4 Answers

delnan provided a good explanation of what dynamic allocation is. +1 to that.

The only thing I want to add is that you use dynamic allocation when you need to. If you have small fixed size of objects, sure allocating them statically is good enough for now. If you have a need for short lived objects within a single function, sure local objects on the stack are great.

Only allocate objects dynamically when the other methods are not good enough. Don't use it just because the language allows it. Sooner or later, you will have a need.

Same goes with C++ templates. I have seen way too many people start plopping templates all over the place just because. Then same people complain how C++ is too convoluted and complicated. C++ has many advanced features, but it is up to the developer to determine when and where to use those features. But when there's no other alternatives, dynamic allocation (and templates) will allow you to do things which otherwise would've been impossible.

credit: DXM
Answered March 10, 2018

Well, you're way wrong about modern languages not using pointers. They call them different things, but they're still pointers under the hood. And they do not even get rid of stack-allocations either, look at .NET for example and see the difference between reference types and value types.

However that's as makes no difference to you. Your problem is that you don;t quite understand what happens with your allocations.

Computers work with a huge lump of memory that they split into 2 groups, the heap and the stack. The stack is used for temporarily passing data between function calls, the heap is a more 'global' storage space for objects. In C++ you get the stack for free by allocating as you've done - Enemy e; is a stack allocation. To use the heap you must create the object and then keep track of where it was allocated yourself, usually by using a pointer. So Enemy* e = new Enemy(); will allocate an Enemy-sized object on the heap, but will be a pointer to that object, no that object itself. Note that e itself is allocated on the stack (every variable has to be stored somewhere after all).

Now thing what happens when you call a function - you know about copy by value and copy by reference? If you've allocated your object on the stack you will get a copy of the object when you call a function, but if you've allocated it on the heap, when you pass the pointer, you will get a copy of the pointer (which, of course, still points to the same object on the heap).

I guess if you're used to Java or .NET's way of handling heap-based objects for you, then the closest you'll get with C++ is to use the smart pointers, shared_ptr<> is probably what you want. Use these instead of raw pointers, or stack objects and you'll be a lot happier. The bonus over GC languages is that you'll also get instant deallocation of them once no-one is using the object anymore (so you can put your enemy death throes routines in the destructor and let the system automatically call it when the enemy is deleted)

credit: gbjbaanb
Answered March 10, 2018

The main difference between C++ and a language like Java, is that in Java, you make a statement like

MyObject b = new MyObject(...);

Objects ARE references. This abstraction has proven to be very powerful in higher level languages. Objects are allocated on the heap and reference counted, taking away our responsibility to de-allocate that memory. The garbage collector simply can tell when an object has no references left to it, and sweeps it clean.

The fact of the matter is, C++ is not AS higher level. An advantage of C++ is that you CAN drop down to very manual memory management to get better performance or tweak certain performance intensive areas of your code.

In C++, if your object is going to be a member of a class, or otherwise passed around, stored, and needed by other classes, etc., you should use a pointer.

MyObject *myClassMemberVariable

Now, there are propenents in C++ that advocate never using raw pointers like this. Libraries like boost and even the standard library make classes like smart pointers and other things that attempt to implement somewhat automatic memory management of resources, by "wrapping" raw pointers, providing different schemes for them to manage the memory.

It all comes down to you having to make your own decision about when to use pointers and how to manage memory. This is mainly what makes C++ harder than other languages (that among... other things). However, it does have its use and its advantages to the projects that use it.

credit: cemulate
Answered March 10, 2018
Your Answer