How do i re-allocate an array in C++ ? equivalent of realloc ?
How do i re-allocate an array in C++ ? equivalent of realloc ?
Either create a new array with the new total of elements, copy the elements from the first and then delete the first, or use STL vector.
zen
I saw your other post and i would suggest that if speed is of the essence that you overload new[] and delete[].In your new new operator use global new to give you a largish chunk of memory and then you can yourself split that into chunks maybe stored in a list or a map and do the memory allocations yourself. I think a call to a function over a call to global new will give you the efficiency you are after.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
If this is in regards to your array question you posted in Gaming, I suggest you take my earlier advice and use a doubly linked list, not an array -- an array is just not a good move for what you're doing. It's much slower than a doubly linked list in all resizing, no matter how you implement it.
Hey,
I know using arrays this way, would slow down the loading of data (world,objects etc.). But that's only done once in the beginning.
After the data has been loaded into the arrays it can be traversed directly.
However, on the other hand, if i use a linked list, it would require linear traversal causing a major slowdown. (traversing all entries before the required one).
right ???
Yeah, but do you need random access?
If you are just going to process a list of objects every frame or something of that nature, it's not as though you need to process the 31st object before you process the 16th, so if you traverse in order and process each in that order, any container you use will give you O(N) access.
SilentStrike is exactly correct.
Arrays and linked lists (properly implemented) are equally fast for sequential access.
Think of them like a pawn and queen on a chessboard, moving in one column from one side of the board to the other. The pawn, in one step, can only move one square, while the queen can move to any of them in a single move. If you need to hit the squares in a random order (like, 7, 3, 1, 6, 4, 5, 8, 2) the queen is much faster than the pawn. If you plan to hit them in the order 1, 2, 3, 4, 5, 6, 7, 8, each is equally fast. And so it is with arrays and linked lists.
LLs have the advantage of constant time resizing, instead of O(N).
All it means is that instead of:
you use something like:Code:for (int i = 0; i < arraySize; i++){ array[i].DoSomething(); }
and both are equally fast.Code:for (list.Start();list.IsValid();list.Next()){ list.Current().DoSomething(); }