Jump to content
  • Advertisement


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


Array redimension

This topic is 6108 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

int array[100];

delete array;
array = new int[1000];


"If it doesn''t fit, force it; if it breaks, it needed replacement anyway."

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You free the old array and allocate memory for the new one. It''s slow but also the only way, since arrays are located in the memory as a continuous sequence.
for (i=0;i<old_height;i++) delete array[i];
delete array;
array = new char *[new_height];
for (int i=0;i<new_height;i++) array[i] = new char [new_width];

Share this post

Link to post
Share on other sites
Just to be picky, MindWipe, that won''t work. You cant declare a static sized array (int array[100]), delete it, and then allocate new memory to it. You might be able to get away with allocating the new memory, but deleting it definitely wont work.


Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I suggest using std::vector. Don''t let the name fool you; it is basically a fancy array.

#include <vector> //note: no .h
using namespace std;

vector<int> vi; //vector of ints

vector<string> vs; //vector of strings (look up std::string)

vi.push_back(5); //puts five onto the end

if(vi.size()==1) cout<<"one"; //prints one

int i = 2 * vi[0]; //i equals 10;

vi[0]=3; //change the 5 to a 3

vi.pop_back();//takes the last element off

//if you #include <algorithm> this sorts your vector:

As with arrays you have to be careful, don''t try to assign or access out of bounds.

Share this post

Link to post
Share on other sites
C++''s standard vector class is great. I would also recommend using it unless you want to learn how to do this yourself. To redimension an array, you want to do something like this:

#include <algorithm>
using std::copy;
using std::swap;
int main()
// p refers to the array that is to be resized
int* p = new int[old_size];
// redimension p to the new size while preserving old values
int* temp = new int[new_size]; // allocate new space
copy(p, p+old_size, temp); // copy old elements over (optional)
swap(p, temp); // swap the pointers, now p refers to the new array
delete [] temp; // deallocate old array
// at some point, we need to deallocate memory for the array
delete [] p;
return 0;

Share this post

Link to post
Share on other sites
Remember that the example I gave you assumes that "new_size" is greater than or equal to "old_size." To wrap this code up into a function we do:

// This just makes "T" an alias for whatever type is really used

template <class T>
void redim(T*& array, int old_size, int new_size, bool preserve_old_elements = true)
// allocate the new array

T* temp = new T[new_size];

// now copy the elements over, if desired

if( preserve_old_elements )
// the number of elements to copy is either the old size or the new size, whichever is smaller

const int elements_to_copy = min(old_size, new_size);

// perform the actual copy

copy(array, array + elements_to_copy, temp);

// swap the pointers, so that "array" now points to the new array

swap(array, temp);

// now we need to deallocate the old array

delete [] temp;

Edited by - null_pointer on December 31, 2001 3:01:40 PM

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!