Dynamic Memory Allocation (why?)

Hullo everyone. I know all about dynamic memory allocation and how it is done and what it accomplishes but honestly I don't see much in the way of application of this technique.

If I'm making a game, it's definitively going to have 10 rooms, maybe 30 enemies, 15 different weapons, and 2 bosses. I don't see a need to dynamically allocate any of this, because all the numbers would have been decided design-time, much before runtime.

The only application I can really see is for something like an std::string object that dynamically adjusts for varying lengths for the user's name or something.. but even then most modern games have a fixed name length.

So could somebody tell me why dynamic memory allocation is so important and relevant to modern development?

Thanks.
Not all programs are games.
You need dynamic array allocation when the size of arrays may depend on the user's input, a file to read etc.
Then talking about strings, think of a program which must save a file given it's full path, how could you decide a proper maximum length?
Back to game programming, a game may want to let the user change the difficulty of the game and this would affect the number of enemies.
You're right in saying that you don't really need to do this as STL provides lots of containers which would do the job for you ( http://www.cplusplus.com/reference/stl/ ) but memory management is one of the things that make C++ a great and flexible language
Another thing you can only achieve via dynamic allocation is polymorphism ( http://www.cplusplus.com/doc/tutorial/polymorphism/ )
I'm not sure why you mentioned polymorphism because that tutorial clearly shows examples that do not use dynamic allocation that still achieve polymorphism (ie the second to last example)

but I'm starting to see what you are talking about anyway. Like if a game wants to load level files that have varying level widths and heights. Difficulty is also a good example.

Thanks for the reply, I appreciate the examples... I see how dynamic memory allocation could be useful. Thanks again.
(polymorphism) The tutorial creates the objects and then sets the pointers in some examples but usually is not the case.
Some other programming languages don't even allow objects to be created without new
Everything Bazzy said plus:

The only kind of games where hard-coding such values is possible is in things like tetris or solitaire. Any moderately complex game (actually, any moderately complex program) tends to move variables further and further into run time. This is because programs should:
1. Be as generic as possible.
2. Avoid unnecessary recompilations.
You can hard-code the rooms, but that means that if the design changes and a room is added, you'll have to rewrite a lot of code.
Sure, you can embed a sprite in the source, but remember that if the sprite changes, you have to recompile and relink.

Some programs can afford to do this (such as the one I mentioned above), but any program complex enough will try to minimize these wasting time on compilations.

For example, no complex enough game is written in C++. What's written in C++ is the engine that handles such things as rendering and sound, and the engine takes its data from resource files (scripts, music and sound files, sprites, maps, models, video). All of this is handled at run time, because modifying the engine for every little change would be ridiculous.

A more simple example is a text editor. The programmer has no way of knowing at compile time how long a file can get. One solution could be to write temporary files once the static memory runs out, but that introduces even more problems. What if there isn't space or the available space is read-only? What do you do when you need to recall an earlier portion of the file? How many temporary files to use? How big to make them?

I could go on, but I think this is enough. If you want to see how important dynamic allocation is, close your tabbing browser of choice, then start it again. See how much memory it's using. Then see again after an hour or two of using it.

EDIT: Man, I really should have refreshed.
Last edited on
it's definitively going to have 10 rooms, maybe 30 enemies, 15 different weapons, and 2 bosses


... for version 1.0 maybe. What if you want more or less? What if you want to scale to more capable or less capable hardware.

The C/C++ virtual machine has a code organised into functions, a call stack (where you declare local data), a default heap and a global data area (where you declare static and global data).

The (default) heap is the only thing that grows as you add more RAM to the box. So the only way to exploit more RAM on a machine is to use the heap.
The C/C++ virtual machine
?
I actual meant virtual computer.

Still confused? As I recall, a virtual computer is the computer that is seen. This is different for each layer and possibly system.

So, for the 6800 programmer, the computer is the Motoralla 6800 instruction set and an understanding of how memory is handled.

The 8086 Macro Assember language programmer sees the computer as something that understands 8086 Assembly Language, has a segmented memory model, is 16 bits and so on.

The OS designer has his own view; and a Windows programmer has a particular view that is different from that of, say, a Unix programmer.

This goes all the way an Excel user where computer is something that stores and manipulates spreadsheets. Or an Oracle database user seeing the machine as something that runs Oracle (irrespective of OS or actual computer).

I can't find the quote now, but I recall reading somewhere where Dennis Ritchie attributed the success of C to it's virtual computer being very close to an actual computer.

So called virtual machines like the Java VM, or a WOW layer, just implement a virtual computer in software.
Topic archived. No new replies allowed.