Skip to main content
gamedev.net
🔒 Locked

dynamic arrays

Started by GOOSEDUMP Oct 7, 2001 at 10:02 PM 8 replies 950+ views
Original Post
GOOSEDUMP
GOOSEDUMP
how do I shrink/enlarge arrays dynamically on the fly? For example, how would I enlarge this array of 20 char to fit 30? char myString[20] = "Hello World";
Oluseyi
Oluseyi
You can''t. You declared a constant string.

For dynamic arrays, do the following:

char *mystring = new char[20];
strcpy( mystring, "Hello World" );

// resize the array but lose the data:
delete mystring;
mystring = new char[30];

// resize the array and preserve the data
char *tmp = new char[20];
strcpy( mystring, tmp );
delete mystring;
mystring = new char[30];
strcpy( tmp, mystring );
delete tmp;

You could fool with assigning one pointer to another:

// assume mystring is a char[20]
char *tmp = new char[30];
strcpy( mystring, tmp );
delete mystring;
mystring = tmp;

This is horribly hackish and is not to be encouraged. It may fail.

Look into std::string (part of the STL). It''s a lot better and easier to use:

using namespace std;
string mystring = "Hellow World";

// want to add ", I''m back!"?
mystring += ", I''m back!";
// mystring is now "Hello World, I''m back!"
Dactylos
Dactylos
quote:
Original post by Oluseyi
You could fool with assigning one pointer to another:

// assume mystring is a char[20]
char *tmp = new char[30];
strcpy( mystring, tmp );
delete mystring;
mystring = tmp;

This is horribly hackish and is not to be encouraged. It may fail.

That should have been:
      
// assume mystring is a char[20]

char *tmp = new char[30];
strcpy(tmp, mystring ); // order of parameters is: dest, source

delete[] mystring; // delete arrays with delete[]

mystring = tmp;

(and similarly for the example above it).

Assuming you did a simple typo, why do you consider this hackish and prone to failure? That looks like the best (char*)-based solution to me (except I would use strncpy). Am I missing something here?

Edited by - Dactylos on October 7, 2001 11:32:10 PM
Oluseyi
Oluseyi
Thanks for the check on param order!

I consider the pointer manipulation hackish because it isn''t immediately clear what''s happening to the casual reader. Of course, good commenting obviates that but I didn''t want to recommend it to someone coming to grips with dynamic arrays. I, personally, do it all the time.
Dactylos
Dactylos
Oh, ok, then I see what you mean. I thought that maybe I was missing something (It''s pretty late here in Sweden ).
Oluseyi
Oluseyi
Well, there''s also the fact that an inexperienced programmer may go ahead and delete tmp and later try to use mystring (whoa, surprise!)
savagerx
savagerx
Olusey, regarding the "delete []array;", is it meant to release array created using "char array = new char[20]"?

In what situations(usually) programmers will use the "new" allocation? is it for dynamic arrays? or otherwise?

The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant
The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant
Martee
Martee
You use ''new'' whenever you want to allocate memory at runtime, instead of at compile-time. Dynamic arrays are one example.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Oluseyi
Oluseyi
Yes, delete[] is meant to delete arrays - all types of arrays. It ensures that each element is deallocated as well as the destructor called on allocated objects. If, for example, you had a class MyClass and you created an array as follows:

MyClass *myclass_array;
myclass_array = new MyClass[20];
for(int i = 0; i < 20; i++)
myclass_array->Initialize();

Then the proper way to deallocate myclass_array is as follows:

for(int i = 0; i < 20; i++)
// do any cleanup the destructor wouldn''t handle
myclass_array->Deinitialize();
delete [] myclass_array;

If you had used plain delete, the destructor wouldn''t be called on each object, leading to unpredictable results.

The new operator can be used for both static and dynamic allocation (which seems to make no sense, but stay with me). If you want to allocate an array, use new. If you want to allocate a buffer, use new. They''re actually the same memory-wise (you allocate a contiguous region of memory and your pointer holds the address of the first element). In other words, so long as you''re using C++, always use new.

Topic Locked

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

Sign in to reply to this topic.