Skip to main content
gamedev.net
🔒 Locked

General array questions...

Started by Shadows Jan 3, 2004 at 12:48 PM 6 replies 550+ views
Original Post
Shadows
Shadows
Okay I suppose that this is quite an easy question for you. I''m having alot of problems with my arrays, when I try to resize them, you know allocate a dynamic array, delete it and then try to regrow it I often get some 0x00000005 error, memory allocation error or something like that, can''t remember the exact words. Anyway I was wondering if anyone could give me some insight in how to grow and shrink arrays, the proper way. I''m quite at loss at how you should use the delete word sometimes delete lArray; works and sometimes delete [] lArray; works and sometimes none of them works... Please give me some good sample code cause I can''t seem to figure this out on my own. Thanks alot friends!
Grofit
Grofit
im not 100% sure because ive never done it before... BUT if you want a dynamic 1d array im sure the below should work...
Lets pretend its an int array

int *Int_Array;

void main()
{
Int_Array = new int[255];
// i think thats the way you would make a new int, just replace
// 255 with whatever number you need in size

delete Int_Array;
// then that should delete what it is and put it back to null
// i think anyway...lol
}

Remember though if you are using * and youve got an array of structures instead of going Int_Array[x].Get_x(); you would use Int_Array[x]->Get_x; ...

if the above is right then just make a function like the one below

void ReSize_Array(int Size)
{
delete Int_Array;

if(Int_Array == NULL)
{
Int_Array = new int[Size];
}
}



[edited by - Grofit on January 3, 2004 1:56:56 PM]
ms291052
ms291052
int *Int_Array;

void main()
{
Int_Array = new int[255];

delete[] Int_Array; //If you create an array you have to delete it using the delete array (delete[]) operator
}
Grofit
Grofit
there we go...
thanks use delete[] then it will work...shazam... its like magic...but not...
Enigma
Enigma
Use std::vector (or std::list, or std::deque or std::set or std::map or std::multiset or std::multimap or one of the nonstandard stl containers).

If you really (and I mean really ) need to use a raw array, the proper way to resize an array (retaining data) is:
	// allocate initial array
Type* array = new Type[size];
// do something with it
...
// increase the size of the array
// first allocate a new array
Type* newArray = new Type[newSize];
// then copy the data across
std::copy(array, array + size, newArray);
// next delete the old array
delete[] array;
// and copy the pointer to newArray back into array
array = newArray;


Enigma
Shadows
Shadows
Thanks alot for your replies everyone I''ll check it all out and if I still can''t get it to work I get back to you again =)

mdias
mdias

BYTE* array = (BYTE*)malloc(100);

BOOL Resize(size_t NewSize)
{
BYTE* pNewArray = realloc( array, NewSize );
if( pNewArray )
{
array = pNewArray;
return TRUE;
}
return FALSE
}



Kam1kaz3
blizzard999
blizzard999
quote:
Original post by Shadows
Okay I suppose that this is quite an easy question for you. I''m having alot of problems with my arrays, when I try to resize them, you know allocate a dynamic array, delete it and then try to regrow it I often get some 0x00000005 error, memory allocation error or something like that, can''t remember the exact words. Anyway I was wondering if anyone could give me some insight in how to grow and shrink arrays, the proper way.



Use std::vector, it will take care of grow/shrink/copy...why rewrite good code?

quote:

I''m quite at loss at how you should use the delete word sometimes
delete lArray; works and sometimes
delete [] lArray; works and sometimes none of them works...



int* pi = new int;
delete pi; // OK

int* pi = new int[n];
delete[]pi; // do not use delete pi !!! new,new[],delete,delete[] are all different operators!!!

see http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.1


Topic Locked

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

Sign in to reply to this topic.