Topic: Arrays and delete - what's wrong?
Author: sabroe@daimi.aau.dk (Morten Sabroe Mortensen)
Date: 9 Aug 92 22:01:13 GMT Raw View
When compiling the program
//BEGIN
main()
{ int *a=new int[100];
delete [100]a;
};
//END
with GNU's gcc under Unix, or djgpp under DOS, I receive the following
warning:
test.cc: In function `int main ()':
test.cc:3: warning: use of array size with vector delete is anachronistic
Why? That warning was totally unexpected! If that's not the way to delete
dynamic arrays, -like the one declared in the second line-, how should it
then be done? Should I ignore the warning? If there isn't any better ways
to it, how do I get rid of the warning? Please help!
--Morten S. M.
Author: pjl@sparc10.cs.uiuc.edu (Paul Lucas)
Date: Mon, 10 Aug 1992 03:28:56 GMT Raw View
In <1992Aug9.220113.21125@daimi.aau.dk> sabroe@daimi.aau.dk (Morten Sabroe Mortensen) writes:
>When compiling the program
>//BEGIN
>main()
>{ int *a=new int[100];
> delete [100]a;
>};
>//END
>with GNU's gcc under Unix, or djgpp under DOS, I receive the following
>warning:
>test.cc: In function `int main ()':
>test.cc:3: warning: use of array size with vector delete is anachronistic
>Why? That warning was totally unexpected! If that's not the way to delete
>dynamic arrays, -like the one declared in the second line-, how should it
>then be done? Should I ignore the warning? If there isn't any better ways
>to it, how do I get rid of the warning? Please help!
*****> See: ARM, pp. 64-65; Stroustrup 2nd ed., pp. 176, 499-500; or
Lippman 2nd. ed., p. 288.
--
- Paul J. Lucas University of Illinois
AT&T Bell Laboratories at Urbana-Champaign
Naperville, IL pjl@cs.uiuc.edu
Author: kocks@jessica.stanford.edu (Peter Kocks)
Date: Mon, 10 Aug 92 23:28:00 GMT Raw View
In article <1992Aug9.220113.21125@daimi.aau.dk> sabroe@daimi.aau.dk (Morten Sabroe Mortensen) writes:
>When compiling the program
>
>//BEGIN
>main()
>{ int *a=new int[100];
> delete [100]a;
>};
>//END
>
>with GNU's gcc under Unix, or djgpp under DOS, I receive the following
>warning:
>
>test.cc: In function `int main ()':
>test.cc:3: warning: use of array size with vector delete is anachronistic
Correct warning (at least IMHO). anachronistic = out of date, no
longer used. It should work, but the correct deletion would simply
be:
delete a;
// pretty simple huh.
I say this is correct, because it is the way it is described in
Strousup (sp?) book. Other compilers may not be up to the standard
and use the old standard.
--Peter Kocks
kocks@chemistry.stanford.edu
Author: amewaldu@cayley.uwaterloo.ca (Andrew Walduck)
Date: Tue, 11 Aug 1992 04:04:24 GMT Raw View
In article <1992Aug10.232800.2941@leland.Stanford.EDU> kocks@jessica.stanford.edu (Peter Kocks) writes:
>In article <1992Aug9.220113.21125@daimi.aau.dk> sabroe@daimi.aau.dk (Morten Sabroe Mortensen) writes:
>>When compiling the program
>>
>>//BEGIN
>>main()
>>{ int *a=new int[100];
>> delete [100]a;
>>};
>>//END
>>
>>with GNU's gcc under Unix, or djgpp under DOS, I receive the following
>>warning:
>>
>>test.cc: In function `int main ()':
>>test.cc:3: warning: use of array size with vector delete is anachronistic
>
>Correct warning (at least IMHO). anachronistic = out of date, no
>longer used. It should work, but the correct deletion would simply
>be:
>
>delete a;
>
>// pretty simple huh.
>
>I say this is correct, because it is the way it is described in
>Strousup (sp?) book. Other compilers may not be up to the standard
>and use the old standard.
>
>--Peter Kocks
> kocks@chemistry.stanford.edu
>
No, the correct answer is:
delete []a;
Why?? Simple rule:
If you allocate with "ptr = new object[20];" delete with "delete [] ptr;".
If you allocate with "ptr = new object;" delete with "delete ptr;".
It makes no difference whether object is an internal type (char int long)
or some user type...
Andrew Walduck
P.S. This has *GOT* to be one of the most insidious bugs in C++...execpt
its a design flaw!!
On some architectures, it "does the right thing", on others, it silently
corrupts memory...so...don't confuse delete and delete[].