Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new int[0] under BC++ 5.0/Win95 will crash!

0 views
Skip to first unread message

Hugues Marty

unread,
Jan 2, 1997, 3:00:00 AM1/2/97
to

I have found that under Borland C++ 5.0 (and 5.01), using the 32 bits
RTL as a DLL, under Win95, if you allocate an empty array with new,
then delete it, it will make your app crash (there is no crash under
WinNT)...

int *a = new int[0];
...
delete[] a;

However, the source code for operator new[]() shows that it checks to
see if the block size is 0, then set it to 1 if so... so it should
work!

Did anyone encounter such a problem, and could give any advice? I
don't want to check all my `new's to see which ones could allocate
empty blocks....

Thanks,
Hugues.
--
Hugues MARTY: ISoft, Chemin de Moulon, F-91190 Gif-sur-Yvette, France
E-mail: hug...@isoft.fr / Phone: +33 1 6941 2777 / Fax: +33 1 6941 2532

Norbert Hache

unread,
Jan 3, 1997, 3:00:00 AM1/3/97
to Hugues Marty


Hi Hugues,

Just curious, why would you allocate new int[0] in the first place ?

Jean-Claude Wippler

unread,
Jan 3, 1997, 3:00:00 AM1/3/97
to

Norbert Hache <nha...@istar.ca> wrote:

>Hugues Marty wrote:
[...]


>> int *a = new int[0];
>> ...
>> delete[] a;
>>
>> However, the source code for operator new[]() shows that it checks to
>> see if the block size is 0, then set it to 1 if so... so it should
>> work!
>>
>> Did anyone encounter such a problem, and could give any advice? I
>> don't want to check all my `new's to see which ones could allocate
>> empty blocks....

[...]


>Just curious, why would you allocate new int[0] in the first place ?

Perhaps to handle the general case?

int n = 0;
int* a = n ? new int [n] : 0;

Should do it.
This is not a problem of BC++, it happens on other systems too.

-- Jean-Claude
______________________________________________________________________

Jean-Claude Wippler http://purl.net/meta4
Meta Four Software CatFish - A freeware disk catalog browser
j...@meta4.nl <ASP> MetaKit - C++ library for persistent data

Andreas Arff

unread,
Jan 6, 1997, 3:00:00 AM1/6/97
to

Jean-Claude Wippler wrote:
>
> Norbert Hache <nha...@istar.ca> wrote:
>
> >Hugues Marty wrote:
> [...]
> >> int *a = new int[0];
> >> ...
> >> delete[] a;
> >>
> >> However, the source code for operator new[]() shows that it checks to
> >> see if the block size is 0, then set it to 1 if so... so it should
> >> work!
> >>
> >> Did anyone encounter such a problem, and could give any advice? I
> >> don't want to check all my `new's to see which ones could allocate
> >> empty blocks....
> [...]
> >Just curious, why would you allocate new int[0] in the first place ?
>
> Perhaps to handle the general case?
>
> int n = 0;
> int* a = n ? new int [n] : 0;
>
> Should do it.
> This is not a problem of BC++, it happens on other systems too.

But it is legal C++ syntax [to new int[0]] and should work on all
compilers calling
themself C++ compilers.

> -- Jean-Claude
Cheers
Andreas
--
Andreas Arff
mailto:a...@ergosoft.no or mailto:and...@online.no
I am just a boring sig

0 new messages