Advertisement

Dynamic Arrays

Started by July 02, 2001 04:31 PM
4 comments, last by LaBasX2 23 years, 7 months ago
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
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
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
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.
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.
I agree with using std::vector. You can append elements to the end of vectors using std::vector.pushback(); I believe.
Thanks for your replies! I''m using the std::vector solution now and it seems to work perfectly. Thanks again!

LaBasX2

This topic is closed to new replies.

Advertisement