Topic: variable length arrays
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/04/08 Raw View
ccwup@$MAILHOST (WU Sheung Chau) writes:
>I also believe deleting an array should use 'delete []'.
>However, I have seen many books on C++ that use 'delete ara' only instead of
>'delete [] ara' if ara points to an array a pre-defined type or class. e.g. int, char etc.
>In those books, they use 'delete []' only when the array is of self-defined
>class.
>
>So, are those books correct?
No. Those books are wrong.
The draft standard is extremely clear on this issue.
>BTW, could you introduce me some books which are more 'standard'?
>( Like 'The C++ programming Language' by B. Stroustrup? <- I haven't read it yet :)
That is certainly well worth reading if you're doing much in the way of C++
programming, and it's about as standard as they get.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: rac@intrigue.com (Robert Coie)
Date: 1995/04/06 Raw View
In article <D6L3zE.730@online.tmx.com.au>, tony@online.tmx.com.au (Tony
Cook) wrote:
: WU Sheung Chau (ccwup@$MAILHOST) wrote:
: : I also believe deleting an array should use 'delete []'.
: : However, I have seen many books on C++ that use 'delete ara' only instead of
: : 'delete [] ara' if ara points to an array a pre-defined type or class.
e.g. int, char etc.
: : In those books, they use 'delete []' only when the array is of self-defined
: : class.
:
: : So, are those books correct?
:
: No they're not.
:
: The behaviour of 'delete ara' on an array allocated with 'new int[]'
: is undefined. This means it could crash your machine (or start
: Doom ;-) ).
:
: Most implementations will accept it, but it's possible a new version
: of a compiler that allows it now will not allow it in the future
: (though unlikely).
Not to mention that if an implementation allows overloading of operator
new[], then even the person who wrote the runtime library can't even
guarantee that it's safe.
: You also need to consider what will happen if you change the type to
: something else for some reason (for example if you adapt existing
: code for use in templates).
:
: --
: Tony Cook - tony@online.tmx.com.au
: 100237.3425@compuserve.com
Robert Coie rac@intrigue.com
Implementor, Intrigue Corporation AppleLink: INTRIGUE
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/04/05 Raw View
WU Sheung Chau (ccwup@$MAILHOST) wrote:
: Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: : > delete ara;
: : That should be `delete [] ara'.
: I learnt C++ by myself so I may have some misconception.
: I also believe deleting an array should use 'delete []'.
: However, I have seen many books on C++ that use 'delete ara' only instead of
: 'delete [] ara' if ara points to an array a pre-defined type or class. e.g. int, char etc.
: In those books, they use 'delete []' only when the array is of self-defined
: class.
: So, are those books correct?
No they're not.
The behaviour of 'delete ara' on an array allocated with 'new int[]'
is undefined. This means it could crash your machine (or start
Doom ;-) ).
Most implementations will accept it, but it's possible a new version
of a compiler that allows it now will not allow it in the future
(though unlikely).
You also need to consider what will happen if you change the type to
something else for some reason (for example if you adapt existing
code for use in templates).
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: ccwup@$MAILHOST (WU Sheung Chau)
Date: 1995/04/03 Raw View
Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: > delete ara;
: That should be `delete [] ara'.
I learnt C++ by myself so I may have some misconception.
I also believe deleting an array should use 'delete []'.
However, I have seen many books on C++ that use 'delete ara' only instead of
'delete [] ara' if ara points to an array a pre-defined type or class. e.g. int, char etc.
In those books, they use 'delete []' only when the array is of self-defined
class.
So, are those books correct?
BTW, could you introduce me some books which are more 'standard'?
( Like 'The C++ programming Language' by B. Stroustrup? <- I haven't read it yet :)
Peter Wu
E8-P)
ccwup@cpccux0.cityu.edu.hk
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/03/29 Raw View
ccwup@$MAILHOST (WU Sheung Chau) writes:
> ....
> .... // your codes
>
> delete ara;
That should be `delete [] ara'.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: michaela@inforamp.net
Date: 17 Mar 1995 20:47:28 -0500 Raw View
I sat down today to do something I thought was possible but I
got an error.
I was able to declare an array with variables as dimensions.
i.e.
int x = 5;
int ara[x];
If this is possible or there is another way to get around the
problem I would really appreciate any information.
Michael Artemiw
Grade 10 FJR H.S.
Toronto Canada
Author: mikeq@primenet.com (Michael Quinlan)
Date: Sun, 19 Mar 1995 08:03:56 MST Raw View
In article <3kde3g$326@inforamp.net> michaela@inforamp.net writes:
>int x = 5;
>int ara[x];
const x = 5;
int ara[x];
+---------------------------------+
| Michael Quinlan |
| mikeq@primenet.com |
| http://www.primenet.com/~mikeq/ |
+---------------------------------+
Author: ccwup@$MAILHOST (WU Sheung Chau)
Date: 20 Mar 1995 09:16:49 GMT Raw View
Michael Quinlan (mikeq@primenet.com) wrote:
: In article <3kde3g$326@inforamp.net> michaela@inforamp.net writes:
: >int x = 5;
: >int ara[x];
: const x = 5;
: int ara[x];
Alternatively, if you want to use array dynamically, try
int x = 5;
int *ara = new char[x];
....
.... // your codes
delete ara;
---
Peter Wu
E8-P)
ccwup@cpccux2.cityu.edu.hk
Author: ccwup@$MAILHOST (WU Sheung Chau)
Date: 20 Mar 1995 09:18:51 GMT Raw View
Sorry, I have made a typing mistake.
: int *ara = new char[x];
should be
int *ara = new int[x];
---
Peter Wu
E8-P)
ccwup@cpccux2.cityu.edu.hk
Author: Alex Pyt'ev <archer@ccp.phys.msu.su>
Date: 20 Mar 1995 13:56:26 GMT Raw View
:> In article <3kde3g$326@inforamp.net> michaela@inforamp.net writes:
:>
:> >int x = 5;
:> >int ara[x];
:>
:> const x = 5;
:> int ara[x];
:>
:>
:> +---------------------------------+
:> | Michael Quinlan |
:> | mikeq@primenet.com |
:> | http://www.primenet.com/~mikeq/ |
:> +---------------------------------+
:>
{
..
int n=10;
int *parr=new int[n];
..
parr[5]=n;
..
delete[] parr;
..
}
Regards, Alex:archer@ccp.phys.msu.su