www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/08/11/00:17:54

From: Leon AT caresystems DOT com DOT au
To: djgpp AT delorie DOT com
Date: Fri, 11 Aug 2000 14:25:21 +1000
MIME-Version: 1.0
Subject: Re: ...but
Message-ID: <39940CD1.5691.11C46C5@localhost>
In-reply-to: <VhEk5.5698$aK5.95670@news1.online.no>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019