Skip to main content
GameDev.net gamedev.net
🔒 Locked

Re-sizing a dynamic array during run-time

Started by DOrmisher Dec 9, 2008 at 10:23 AM 5 replies 950+ views
Original Post
DOrmisher
DOrmisher
What I'm doing is not strictly resizeing the array (I don't think that is possible. I have a set class and objects are stored in a dynamic array. The array is initialised in the set's constructor so when an item is inserted and there is no room left the array is doubled. CObject is part of a linked list with a pointer to next on the list. I have been attempting it like so: The aray has been initialised like so...

CObject< Type >* data = new CObject< Type >[ m_Size ];
but when the array is full we want to double the array...

CObject< Type >* data = new CObject< Type >[ m_Size*2 ];
		
for( int i=0; i<m_Size; i++ )
{
  data = m_Data;
}

delete m_Data;
m_Data = data;

m_Size*=2;
The for loop copies all the data fine (although it looks like it is copying mem loc's instead of values). If I add a watch to data and m_Data I can see that before I have deleted m_Data, data's values are correct. However, once I delete m_Data the values in data (except the first on the list) all change as if they have been deleted. I've not a clue why this is happening. If anyone knows a better way to create a new dynamic array and copy the values over I'm all ears.
She got a hat and all that hat says is asshole
SiCrane
SiCrane
Just use std::vector. It'll handle all these details for you.
DOrmisher
DOrmisher
cant do that, im a student, gotta make my own container.
She got a hat and all that hat says is asshole
DOrmisher
DOrmisher
all CObject has is default constructor
She got a hat and all that hat says is asshole
DOrmisher
DOrmisher
I don't think anybody is listening but I've been messing around with dynamic arrays and found that if I do this:

int* i; i = new int[3];i[0] = 0;i[1] = 1;i[2] = 2;for(int a=0; a<3; a++)  cout << i[a];

It outputs the first int fine, then the rest are gibberish.
However if I initialise it like this:
int* i; i = new int[3];for(int a=0; a<3; a++)  i[a] = a;for(int a=0; a<3; a++)  cout << i[a];

It outputs 123!! WHY??!!
She got a hat and all that hat says is asshole
Evil Steve
Evil Steve
Quote:
Original post by DOrmisher
I don't think anybody is listening but I've been messing around with dynamic arrays and found that if I do this:

int* i; i = new int[3];i[0] = 0;i[1] = 1;i[2] = 2;for(int a=0; a<3; a++)  cout << i[a];

It outputs the first int fine, then the rest are gibberish.
However if I initialise it like this:
int* i; i = new int[3];for(int a=0; a<3; a++)  i[a] = a;for(int a=0; a<3; a++)  cout << i[a];

It outputs 123!! WHY??!!
Because your code is wrong. That code works fine, post the actual code you'er having problems with, or try to make a minimal sample that demonstrates the problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.