Archived

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

Dynamic Arrays

This topic is 6006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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