🔒 Locked
dynamic arrays
Advertisement
Original Post
You can''t. You declared a constant string.
For dynamic arrays, do the following:
You could fool with assigning one pointer to another:
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:
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!"
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:
|
(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
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.
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.
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
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
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:
Then the proper way to deallocate myclass_array is as follows:
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.
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.
Advertisement