Mail Archives: djgpp/2000/08/11/00:17:54
On 10 Aug 2000, at 22:38, Vermin wrote:
> but is it possible to have an array (or a pointer to an array) which doesn't
> have a a given size (or doesn't really exist) until you define it by making
> space for it in the mem...
pointer to an array is really a pointer to its first element (well...)
thus
char*my_array;//a pointer to an array if you like
//some time later in code you find out you want your array to be
//250 chars long
if(my_array)delete[]my_array;//leak protection
my_array=new char[250];
now if you allocated wiht malloc()
then
if(my_array)free(void*)my_array;
my_array=(char*)malloc(250);
PS i have not compield and really teste the second verdion (esp where the casts
are concerned so will have to try - but generally speaking the first method
is pref for c++ since new and delete is more type verified so to speak :-)
Leon.
another thing - if my_array is a data member of your class
check in your destructor if it is not 0 and then free memory
while we are on that subject - do initialise my_array to 0 in your constructor
otherwhise the first leak protection check might think that my_array has been allocated some mem.
- Raw text -