Dynamically creating elements of a struct or an instance of a class.

Started by
2 comments, last by Digivance 11 years, 3 months ago

Hello, like the title states, how does one dynamically creating elements of a struct or an instance of a class. For example, I have a sword, this sword is only ever created when i need it; let's use minecraft here. In minecraft, you can have an infinite number of swords when you need them. I do not have any code that i have previously worked on, I would just like some guidance on what i can do for this; do not give me source code!

Advertisement

You could use a simple dynamic array, or an std::vector or some other dynamic data structure. This way you can add and remove elements freely, at any time, without being worried about running out of space (up to a limit, obviously, there is no such thing as infinite memory).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You'd use something like a 'std::vector<Sword*> swords' and then to add to it 'swords.push_back(new Sword())'

Just make sure that when you're done with the vector you delete all the memory allocated (iterate through the vector and call delete on each element).

If you've got a c++11 compiler (VS2012/GCC 4.7) you can use smart pointers (unique_ptr or shared_ptr) which will handle the memory deletion for you

Let me start by saying yes, use STL objects such as vectors or deque's. They are well written and are probably the more stable memory manager objects than an indie is likely to write from scratch. Beyond that dynamic memory is dynamic memory. Simply create a pointer to a new allocation of memory the size of the object, instantiate it with an instance of said object, use as needed and release, zero or delete it from memory when your done. Be careful though, when your releasing objects from memory if you miss something you stand a very high chance of "leaking" memory. Minecraft is written in java which has a pretty good garbage collector that will dispose of unused memory blocks for you (as does C#). If you are using C++ you're on your own here, memory stays active until you release it. If you forget to release or zero the memory but lose the pointer you leak. The more you leak the more you crash, the less you sell, the more you give up... Snowball effect.

In short, if your using C++ use STL, if your using Java or C++ hit the books some more and learn lists & array's, remember to null out variables you don't need any more. It's always good practice to reuse memory when possible instead of releasing and re-allocating. I hate to be that guy that tells you to go read the manual but if the answers given thus far aren't making sense or seem a little confusing your not ready young coder, hit the books and practice with smaller scale tests that might emulate what your trying to do. Get good debuggers, echo messages to console or log files so that you can monitor and ensure what your doing is working as needed and once you have it move it into a reusable library (static or dynamic doesn't matter). Dynamic memory usage is complicated for a beginner, it's complicated for a master to get right but overall it's a pretty standard practice that you should understand before jumping into any large coded projects such as games or networked software.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

This topic is closed to new replies.

Advertisement