C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do you new/delete an array?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Daryle Walker
Guest





PostPosted: Thu Jan 29, 2004 9:52 am    Post subject: How do you new/delete an array? Reply with quote



If you have a single object of type T, you can do:

T * t1 = new T;
T * t2 = new T( Whatever );
T * t3 = new DerivedT;
T * t4 = new DerivedT( Whatever );
//...
delete t4;
delete t3;
delete t2;
delete t1;

The "T" type needs to be a class and have a virtual destructor for "t3"
and "t4" to work, obviously.

If you want an array of T, you can do:

T * t5 = new T[ SomeNumber ];
//...
delete [] t5;

What about definite arrays? What if T is array of exactly 5 X objects?
I guess I would use the "t1" syntax above (or "t2" only for copy
construction), because I'm treating it as a _single_ T object, not five
X objects.

Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

How would I do any unspecified array of X-five-packs (without a "T"
typedef)?

X (*t7)[5] = new What_the_Heck_Goes_Here2;
//...
delete [] t7;

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Thu Jan 29, 2004 3:12 pm    Post subject: Re: How do you new/delete an array? Reply with quote



Hi,

Quote:
T * t1 = new T;
T * t2 = new T( Whatever );
T * t3 = new DerivedT;
T * t4 = new DerivedT( Whatever );
//...
delete t4;
delete t3;
delete t2;
delete t1;

The "T" type needs to be a class and have a virtual destructor for "t3"
and "t4" to work, obviously.

They need to be classes or structs to be precise. Destructors only have
to be virtual in case you want to delete them thru T *, not thru DerivedT *,
as you did.

Quote:
If you want an array of T, you can do:

T * t5 = new T[ SomeNumber ];
//...
delete [] t5;

What about definite arrays? What if T is array of exactly 5 X objects?

No difference.

T * t5 = new T[5];
delete[] t5;

Or, in case you want the objects on the stack rather than the heap:

T t5[5];

then, "no delete since no new". The objects are automatic and are
deleted as soon as program flow leaves the closing brace (the block)
t5 was defined within. Access to elements of the array looks the same
in both cases. "t5[2]" would refer to the third element of the array.

Quote:
I guess I would use the "t1" syntax above (or "t2" only for copy
construction), because I'm treating it as a _single_ T object, not five
X objects.

Also, how would I do this without typedefs?

You're either confused, or you lost me. The pointer doesn't carry any
information around about how "long" the array is it points to. You can
mimic this to some degree with additional care, but why? It is the
matter how the pointer is used. It doesn't know by itself whether it
points to a single element, or a full array. You have to know when writing
the program. And when deleting the array, of course, instead of deleting
a single element.

So long,
Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Thu Jan 29, 2004 6:52 pm    Post subject: Re: How do you new/delete an array? Reply with quote



Daryle Walker wrote:
Quote:
If you have a single object of type T, you can do:

T * t1 = new T;
T * t2 = new T( Whatever );
T * t3 = new DerivedT;
T * t4 = new DerivedT( Whatever );
//...
delete t4;
delete t3;
delete t2;
delete t1;

The "T" type needs to be a class and have a virtual destructor for "t3"
and "t4" to work, obviously.

If you want an array of T, you can do:

T * t5 = new T[ SomeNumber ];
//...
delete [] t5;

What about definite arrays? What if T is array of exactly 5 X objects?
I guess I would use the "t1" syntax above (or "t2" only for copy
construction), because I'm treating it as a _single_ T object, not five
X objects.

You can't do this. Array types just don't have the same status as other
object types. However, you can put your arrays in wrapper classes and
avoid this annoying problem. One implementation of this can be found at
<http://www.boost.org/libs/array/>.

Quote:
Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

Typedefs don't make a difference; the choice of scalar vs array new is
not dependent on the syntax of the type-id. You must use:
X * t6 = new X[5];
delete [] t6;

Quote:
How would I do any unspecified array of X-five-packs (without a "T"
typedef)?

X (*t7)[5] = new What_the_Heck_Goes_Here2;
//...
delete [] t7;

Use:
X (*t7)[5] = new X[SomeNumber][5];

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Allan W
Guest





PostPosted: Tue Feb 03, 2004 11:35 am    Post subject: Re: How do you new/delete an array? Reply with quote

Quote:
Daryle Walker wrote:
Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote
Quote:
Typedefs don't make a difference; the choice of scalar vs array new is
not dependent on the syntax of the type-id. You must use:
X * t6 = new X[5];
delete [] t6;

Is that correct?

typedef Foo[5] Foo5; // 1
Foo5 *f = new Foo5; // 2
Foo5 g[3]; // 3
...
delete f; // 4
Is (2) considered a scalar new (calls operator new())? Or is it considered
an array new (calls operator new[]())?

Obviously (3) allocates 15 Foo objects. But isn't the size of the array
3 elements? Doesn't sizeof(g)/sizeof(g[0]) == 3?

Should (4) have used delete[]? Why or why not?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Tue Feb 03, 2004 6:50 pm    Post subject: Re: How do you new/delete an array? Reply with quote

Hi,

Quote:
Is that correct?

typedef Foo[5] Foo5; // 1
Foo5 *f = new Foo5; // 2
Foo5 g[3]; // 3
...
delete f; // 4
Is (2) considered a scalar new (calls operator new())? Or is it considered
an array new (calls operator new[]())?

No, it's invalid. You can't typedef to an array. Line 1 is invalid already.
You could go around that by

struct Foo5 {
Foo myarray[5];
}

In that case, allocation would go thru the scalar new, and deletion thru
scalar delete, and it would call the implicit constructors/destructors of
the Foo5 class which calls the default-constructors/destructors of myarray,
once for each element in the array, being the (only) member of Foo5.

Quote:
Obviously (3) allocates 15 Foo objects. But isn't the size of the array
3 elements? Doesn't sizeof(g)/sizeof(g[0]) == 3?

With Foo5 replaced by the above correction, it constructs three Foo5 objects
containing five Foo objects, calling (after all) the constructor of Foo
fifteen times, three times in groups of five.

Quote:
Should (4) have used delete[]? Why or why not?

As //1 is invalid, there's no definite answer. With the suggested fix, it
is a scalar deletion since it is a scalar allocation. You allocate *one* Foo5
object. That this object requires five constructor calls to Foo is not
an issue.

Similarly,

struct Point { int x,y; };

describes *one* object, though it contains two members. Just that the implicit
constructor of "int" does nothing, possibly unlike the constructor of Foo.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Feb 03, 2004 6:56 pm    Post subject: Re: How do you new/delete an array? Reply with quote

[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote in message
news:<7f2735a5.0402021504.4647c570 (AT) posting (DOT) google.com>...

Quote:
Daryle Walker wrote:
Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote
Typedefs don't make a difference; the choice of scalar vs array new
is not dependent on the syntax of the type-id. You must use:
X * t6 = new X[5];
delete [] t6;

Is that correct?

Yes.

Quote:
typedef Foo[5] Foo5; // 1

Syntax error. You meant:

typedef Foo Foo5[ 5 ] ;

I suppose.

Quote:
Foo5 *f = new Foo5; // 2

Type error. None of the comilers I use allow this line.

Did you mean:

Foo* f = new Foo5 ;

That's legal.

Quote:
Foo5 g[3]; // 3
...
delete f; // 4

Is (2) considered a scalar new (calls operator new())? Or is it
considered an array new (calls operator new[]())?

Array new. The obvious hint is in the type error. A new expression
"new T" returns a T* if T is a scalar, but something different if T is
an array. Since we get a compiler error if we try to initialize a Foo5*
with new Foo5, Foo5 is not a scalar.

Quote:
Obviously (3) allocates 15 Foo objects. But isn't the size of the
array 3 elements?

Yes. And each element is an array.

Quote:
Doesn't sizeof(g)/sizeof(g[0]) == 3?

Yes. And sizeof( g[0] ) == sizeof( Foo5 ) == 5 * sizeof( Foo ).

Quote:
Should (4) have used delete[]?

Yes. Otherwise, you have undefined behavior.

Give Foo a non-trivial destructor, and you'll get some very undesirable
undefined behavior on most systems.

Quote:
Why or why not?

Because the standard says so. The same as it says that:

new Foo(&(*)())[ 5 ]

is not an array new, and should not take a delete[]. The key is in the
type you have to assign it to:
Foo (&(**ppf)())[ 5 ] = new Foo(&(*)())[ 5 ] ;

In this case, using typedefs is clearer:

typedef Foo Foo5[ 5 ] ;
typedef Foo5& Fnc() ;
typedef Fnc* FncPtr ;

FncPtr* pf = new FncPtr ;

Whether new is an array new or no depends on the type being newed.
Typedef does not introduce a new type.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dietmar Kuehl
Guest





PostPosted: Wed Feb 04, 2004 10:41 am    Post subject: Re: How do you new/delete an array? Reply with quote

[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote:
Quote:
Is that correct?

typedef Foo[5] Foo5; // 1

I suppose, this line should read

typedef Foo Foo5[5]; // 1

Quote:
Foo5 *f = new Foo5; // 2
Foo5 g[3]; // 3
...
delete f; // 4

This line shall read

delete[] f; // 4

'Foo5' is an abbreviation for 'Foo[5]' and thus allocates an array object.
Hence, you need to use 'delete[]' when releasing the objects. The typedef
does not change this.

Quote:
Is (2) considered a scalar new (calls operator new())? Or is it considered
an array new (calls operator new[]())?

The latter.

Quote:
Obviously (3) allocates 15 Foo objects. But isn't the size of the array
3 elements? Doesn't sizeof(g)/sizeof(g[0]) == 3?

This is correct. However, it is an array of arrays: you could have used

Foo g[3][5];

instead (I hope I got the indicies correct...). Essentially, a typedef name
can be simply replaced by the corresponding type - well, you need to
distribute the funny specifiers like '[n]' and 'const' appropriately. For
example:

typedef int* Ptr;
const Ptr ptr = 0;

If you want to write the last line without the typedef, you need to write

int* const ptr = 0;

which is, of course, obvious if you had written

Ptr const ptr = 0;

in the first place... However, this is an entirely different issue with
typedefs :-)

Quote:
Should (4) have used delete[]? Why or why not?

Yes, it should because 'new Foo5' allocate an array object. Array objects
shall be released with 'delete[]'.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marcin 'Qrczak' Kowalczyk
Guest





PostPosted: Wed Feb 04, 2004 10:50 am    Post subject: Re: How do you new/delete an array? Reply with quote

On Tue, 03 Feb 2004 13:50:44 -0500, Thomas Richter wrote:

Quote:
typedef Foo[5] Foo5; // 1

No, it's invalid. You can't typedef to an array.

You can, only the syntax was wrong: typedef Foo Foo5[5];
An array created by 'new Foo5' must be deleted by delete[].

--
__("< Marcin Kowalczyk
__/ [email]qrczak (AT) knm (DOT) org.pl[/email]
^^ http://qrnik.knm.org.pl/~qrczak/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Feb 04, 2004 10:52 am    Post subject: Re: How do you new/delete an array? Reply with quote

Allan W wrote:
Quote:
Daryle Walker wrote:
Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote
Typedefs don't make a difference; the choice of scalar vs array new is
not dependent on the syntax of the type-id. You must use:
X * t6 = new X[5];
delete [] t6;

Is that correct?

Ask your compiler if you don't believe me.

Quote:
typedef Foo[5] Foo5; // 1

Presumably you mean "typedef Foo Foo5[5];".

Quote:
Foo5 *f = new Foo5; // 2
Foo5 g[3]; // 3
...
delete f; // 4
Is (2) considered a scalar new (calls operator new())? Or is it considered
an array new (calls operator new[]())?

It's an array new, like I said, so the new-expression has type Foo *
and the initialisation is ill-formed.

Quote:
Obviously (3) allocates 15 Foo objects. But isn't the size of the array
3 elements? Doesn't sizeof(g)/sizeof(g[0]) == 3?

Correct.

Quote:
Should (4) have used delete[]? Why or why not?

Supposing you also fix (1) and (2), (4) should use array delete because
(2) uses array new.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Feb 05, 2004 3:37 am    Post subject: Re: How do you new/delete an array? Reply with quote

In message <bvod67$lg7$1 (AT) mamenchi (DOT) zrz.TU-Berlin.DE>, Thomas Richter
<thor (AT) cleopatra (DOT) math.tu-berlin.de> writes
Quote:
Is that correct?

typedef Foo[5] Foo5; // 1
Foo5 *f = new Foo5; // 2
Foo5 g[3]; // 3
...
delete f; // 4
Is (2) considered a scalar new (calls operator new())? Or is it
considered
an array new (calls operator new[]())?

No, it's invalid. You can't typedef to an array. Line 1 is invalid
already.

Line one is invalid because the syntax is wrong, not because you cannot
typedef to an array:

typedef Foo Foo5[5];

makes Foo5 a typename for an array of five Foo. Of course there are
other problems, such as typedef does not create a new type which is why
the other lines are problematical.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daryle Walker
Guest





PostPosted: Fri Feb 06, 2004 10:17 am    Post subject: Re: How do you new/delete an array? Reply with quote

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> wrote:

Quote:
Daryle Walker wrote:
[SNIP]
If you want an array of T, you can do:

T * t5 = new T[ SomeNumber ];
//...
delete [] t5;

What about definite arrays? What if T is array of exactly 5 X objects?
I guess I would use the "t1" syntax above (or "t2" only for copy
construction), because I'm treating it as a _single_ T object, not five
X objects.

You can't do this. Array types just don't have the same status as other
object types. However, you can put your arrays in wrapper classes and
avoid this annoying problem. One implementation of this can be found at
http://www.boost.org/libs/array/>.

We should fix this for the next version of C++. (It's a core language
change.)

Quote:
Also, how would I do this without typedefs?

X (*t6)[5] = new What_the_Heck_Goes_Here;
//...
delete t6; // better not be "delete [] t6"!

Typedefs don't make a difference; the choice of scalar vs array new is
not dependent on the syntax of the type-id. You must use:
X * t6 = new X[5];
delete [] t6;

You changed the type! I had a pointer to a single definiate array, you
used a pointer to a single object to fake an indefinate array. It seems
that so many people thought/used the latter syntax that no one bothered
to invent C++ rules to make the former work.

Quote:
How would I do any unspecified array of X-five-packs (without a "T"
typedef)?

X (*t7)[5] = new What_the_Heck_Goes_Here2;
//...
delete [] t7;

Use:
X (*t7)[5] = new X[SomeNumber][5];

If we can do this, why can't we do it for a single definate-array object
(without using the above syntax with SomeNumber == 1).

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daryle Walker
Guest





PostPosted: Fri Feb 06, 2004 10:20 am    Post subject: Re: How do you new/delete an array? Reply with quote

Thomas Richter <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote:

[SNIP]
Quote:
If you want an array of T, you can do:

T * t5 = new T[ SomeNumber ];
//...
delete [] t5;

What about definite arrays? What if T is array of exactly 5 X objects?

No difference.

T * t5 = new T[5];
delete[] t5;
[SNIP]


I've think you've misunderstood.

Quote:
I guess I would use the "t1" syntax above (or "t2" only for copy
construction), because I'm treating it as a _single_ T object, not five
X objects.

Also, how would I do this without typedefs?

You're either confused, or you lost me. The pointer doesn't carry any
information around about how "long" the array is it points to. You can
mimic this to some degree with additional care, but why? It is the
matter how the pointer is used. It doesn't know by itself whether it
points to a single element, or a full array. You have to know when writing
the program. And when deleting the array, of course, instead of deleting
a single element.

Looking at:

W a;
W b[ N ]; // N >= 1, of course

The objects "a" and "b" have different types. I think this is true even
if "N" is one! Object "a" is of type "W", object "b" is of type "N-pack
of W".

Your confusion is from the classic "feature v. mis-feature": arrays
objects almost always decay to a value of a pointer to the array's first
element. You are continuously using this effect, I want to know how to
do allocations _without_ this effect!

If I do this:
typedef W X[N]; // N is a compile-time constant

X * c = new X;
//...
// Delete "c" here

Should I use "delete c" or "delete [] c"? You think it should be the
latter, because multple "W" objects are used. I think it should be the
former, because only a _single_ "X" object is _directly_ allocated.

Note that the array-to-first-element-pointer effect is _not_ being used.
(I hope; if I'm wrong, then C++ is really broken.) The "X *" type
should be "W (*)[N]" in expanded form, not "W *"! Note that the exact
number of sub-elements, "N", is always known.

My other question: if the above code is legal, then how do I do it
without the "X" typedef?

--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Fri Feb 06, 2004 8:13 pm    Post subject: Re: How do you new/delete an array? Reply with quote


Hi,

Quote:
No difference.

T * t5 = new T[5];
delete[] t5;
[SNIP]

I've think you've misunderstood.

Looking at:

W a;
W b[ N ]; // N >= 1, of course

The objects "a" and "b" have different types.

Yes, but in

W *a; // <--- is this what you mean?
W b[N];

the objects a and b also have different types, but we have an automatic
conversion from b to the type of a.

Quote:
I think this is true even
if "N" is one! Object "a" is of type "W", object "b" is of type "N-pack
of W".

Yes, so?

Quote:
Your confusion is from the classic "feature v. mis-feature": arrays
objects almost always decay to a value of a pointer to the array's first
element. You are continuously using this effect, I want to know how to
do allocations _without_ this effect!

It's in the core of the language. You can't "bypass" this effect since it
is an automatic conversion that is performed, and operator new always
returns pointers, never arrays.

Quote:
If I do this:
typedef W X[N]; // N is a compile-time constant

X * c = new X;
//...
// Delete "c" here

The construction above doesn't work and won't compile. new X returns an int *,
not an int [5], and the former cannot be assigned to the latter. There's
only an automatic conversion that works in the opposite direction. Similarly,
new int[5] also returns an int * and not an int [5].

Quote:
Should I use "delete c" or "delete [] c"? You think it should be the
latter, because multple "W" objects are used. I think it should be the
former, because only a _single_ "X" object is _directly_ allocated.

Rather irrelevant since the example cannot work since there is no implicit
conversion for new that would work.

For completeness: Passing an int [5] into delete performs an automatic
conversion to an int (*). The semantic whether an array or a single element
is passed comes instead from the operator you call. If it is delete, then
a non-array is disposed. If delete[] is used, an array gets disposed.

For example, the following code *does* compile:

int main(int argc,char **argv)
{
typedef int foo[5];
foo bar;

delete bar;

return 0;
}

This will, of course, crash since you dispose an array on the stack with
the heap deallocation. A suitable compiler should generate a warning, though.

If you replace delete by delete[] it will also compile, but you also get
a warning here, e.g. g++-3.2 says:

test.cpp: In function `int main(int, char**)':
test.cpp:10: warning: deleting array `int bar[5]'

Clearly! Since an object of type int [5] can never be created by new. The
type of objects new returns is always an int *, and never an int [5].
Nevertheless, the program is syntactically correct (thus, no error) since
an automatic conversion is performed.

Now, if we change the program as follows:

foo *bar;

I read it as a pointer to an array (or a single element) of integers
that is built up in groups of five integers each. Thus, to legally
create such a thing, use new int[5] (or rather, new int[5 * N] which
presents the idea more clearly) and cast, since new returns an int
*. This is by the typedef equivalent to new foo, and thus the allocation
of an array type. In fact, g++ calls operator new[] on this.
Since everything created by new [] must be deleted by delete[], it
is clear which delete to use here. The array type.

Quote:
Note that the array-to-first-element-pointer effect is _not_ being used.

It is. "new" always performs this. It only returns pointers, never arrays.
The only way to get an array type is to create one on the stack.

Quote:
(I hope; if I'm wrong, then C++ is really broken.) The "X *" type
should be "W (*)[N]" in expanded form, not "W *"! Note that the exact
number of sub-elements, "N", is always known.

My other question: if the above code is legal, then how do I do it
without the "X" typedef?

The above code is syntactically not legal because the return type of the
operator new is different from what you expect. You can cast of course,
but then, since you allocated an array, you have to delete an array.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marcin 'Qrczak' Kowalczyk
Guest





PostPosted: Fri Feb 06, 2004 8:15 pm    Post subject: Re: How do you new/delete an array? Reply with quote

On Fri, 06 Feb 2004 05:20:21 -0500, Daryle Walker wrote:

Quote:
If I do this:
typedef W X[N]; // N is a compile-time constant

X * c = new X;

This is wrong: new X has type W *, not X *. Just like new W[N] has.

Quote:
// Delete "c" here

Should I use "delete c" or "delete [] c"?

So it's delete[] c.

Quote:
(I hope; if I'm wrong, then C++ is really broken.)

Maybe it is, but it's how things are. Worse is that C++ uses the same typ=
e
for pointers to single objects and pointers to multiple objects, instead
of two pointer kinds with an implicit conversion from multiple to single.
Only the multiple kind would support pointer arithmetic and only the
single kind would support the conversion to the pointer to a superclass.
delete vs. delete[] would be distinguished by the type of argument. But
I'm afraid it's too late to change this.

Quote:
The "X *" type should be "W (*)[N]" in expanded form, not "W *"!

It is. But the 'new' expression has a different type depending on whether
the object we are creating is an array or not.

--
__("< Marcin Kowalczyk
__/ [email]qrczak (AT) knm (DOT) org.pl[/email]
^^ http://qrnik.knm.org.pl/~qrczak/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Tue Feb 10, 2004 12:43 am    Post subject: Re: How do you new/delete an array? Reply with quote

Hi,

Quote:
Maybe it is, but it's how things are. Worse is that C++ uses the same type
for pointers to single objects and pointers to multiple objects, instead
of two pointer kinds with an implicit conversion from multiple to single.

I wouldn't call this the major flaw of C++. After all, a pointer is
just that: A memory location where an object of a specific type can be
found. Whether it is one or several objects shouldn't be the matter
of the pointer type. Thus, whether an object "foo" follows object
"bar" in memory is the matter of the program design and not the matter
of the language, at least *if* you play "arrays with pointers" and if you
look at the world in the "C" way.

I agree that C style arrays (and here's the real problem, a C style array
is not really a data type, but rather a "design guide" how to place multiple
objects in memory) are confusing, hacky but provide a very low-complexity.

If the lack of a clean design worries you, I would rather recommend using
the vector class of the STL which does all that and hides "the old mess"
from the programmer.

Quote:
Only the multiple kind would support pointer arithmetic and only the
single kind would support the conversion to the pointer to a superclass.
delete vs. delete[] would be distinguished by the type of argument. But
I'm afraid it's too late to change this.

No, it's not too late to fix the code. Why insist of using "raw C style
arrays" if there are standard classes that implement the array idea more
clearly if you don't like the "C-ish" raw arrays?

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.