Archived

This topic is now archived and is closed to further replies.

LaBasX2

Dynamic Arrays

Recommended Posts

LaBasX2    122
Hi! I need a way for a engine to create a dynamic array with a variable number of elements. It should be possible to append elements to the array, thus I need some sort of list. Since I''m still a C++ newbie, I don''t know how to do that. Could someone please give me some hints? What''s the best way to achieve that? Thanks in advance LaBasX2

Share this post


Link to post
Share on other sites
Martee    476
A dynamic array can be done like so:
int* myDynamicArray = new int[numberOfItems];

But if you want to be able to easily append elements (and hence, resize the array), you might want to look into std::vector.

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
BLZBub    122
A far better way to implement a dynamic array is to use a linked list. Look this up on the net for for details and some tutorials.

The basics are that the list allows you to add, remove and insert values into the list at runtime very easily.

If you are stuck, let me know and I''ll point you in the right direction and show you some source code.

Share this post


Link to post
Share on other sites
Stratowagon    122
Linked lists are absolutely terrible for producing cache misses though... heavy dependance on them can become a big bottleneck in performance. Of course, only if performance is one of your goals... if it is not, then they do make life easier.

Share this post


Link to post
Share on other sites
Farnz    122
I agree with using std::vector. You can append elements to the end of vectors using std::vector.pushback(); I believe.

Share this post


Link to post
Share on other sites
LaBasX2    122
Thanks for your replies! I''m using the std::vector solution now and it seems to work perfectly. Thanks again!

LaBasX2

Share this post


Link to post
Share on other sites