what does a new A[10] return?

J

Jess

Hello,

Does the statement above return a pointer to an array of As, or, a
pointer to the initial A object? Since the statement

new A

returns a pointer to A, then new A[10] should return a pointer to an A
array, i.e. its type is something like

A []*

However, it seems it returns an A pointer, i.e. A*. Does the standard
say "new A[...]" always returns a pointer to the first A object?

Thanks,
Jess
 
J

Jacek Dziedzic

Jess said:
Hello,

Does the statement above return a pointer to an array of As, or, a
pointer to the initial A object? Since the statement

The latter.
new A

returns a pointer to A,

Yes.
then new A[10] should return a pointer to an A
array, i.e. its type is something like

A []*

No.
However, it seems it returns an A pointer, i.e. A*.

Yes.
Does the standard
say "new A[...]" always returns a pointer to the first A object?

I believe so.

HTH,
- J.
 
J

James Kanze

Does the statement above return a pointer to an array of As, or, a
pointer to the initial A object? Since the statement
returns a pointer to A, then new A[10] should return a pointer to an A
array, i.e. its type is something like

You mean A(*)[]. It should, but it doesn't. This is a major
defect in C++, inherited from C. In practice, it doesn't
matter; in well over 15 years of C++, I've never, ever found a
case where you might want to do a new A[N], or use array new in
general.
However, it seems it returns an A pointer, i.e. A*. Does the
standard say "new A[...]" always returns a pointer to the
first A object?

Yes. Sort of a drag, but as I say, since "new A[...]" is
useless anyway, it doesn't matter.
 
M

Markus Schoder

Does the statement above return a pointer to an array of As, or, a
pointer to the initial A object? Since the statement
returns a pointer to A, then new A[10] should return a pointer to an A
array, i.e. its type is something like

You mean A(*)[]. It should, but it doesn't. This is a major defect in
C++, inherited from C. In practice, it doesn't matter; in well over 15
years of C++, I've never, ever found a case where you might want to do a
new A[N], or use array new in general.
However, it seems it returns an A pointer, i.e. A*. Does the standard
say "new A[...]" always returns a pointer to the first A object?

Yes. Sort of a drag, but as I say, since "new A[...]" is useless
anyway, it doesn't matter.

I can see one reason to use it for scalar/POD types in certain
situations: To avoid the cost of initialization that std::vector has.
 
O

Old Wolf

You mean A(*)[]. It should, but it doesn't. This is a major
defect in C++, inherited from C.

If you consider it a defect then what fix would
you propose?

new[] has to support the quantity not being
known at compile-time, so there's no way it
can return a pointer to array (whose size
must be known at compile time, otherwise you
couldn't assign it to anything).
 
J

James Kanze

[...]
Yes. Sort of a drag, but as I say, since "new A[...]" is useless
anyway, it doesn't matter.
I can see one reason to use it for scalar/POD types in certain
situations: To avoid the cost of initialization that std::vector has.

Have you ever actually measured a case where new A[...] was
faster than std::vector? You have to initialize the elements at
some time anyway.
 
J

James Kanze

You mean A(*)[]. It should, but it doesn't. This is a major
defect in C++, inherited from C.
If you consider it a defect then what fix would
you propose?

Nothing. The issue was discussed in the committee before the
adoption of the standard. Anything sufficient to fix it would
break C compatibility. And solutions like std::vector and
boost::array work fine; you can pretty much ignore the existance
of array new, all of the implicit conversions surrounding C
style arrays, and the fact that they aren't first class objects,
by simply not using them. In fact, about the only reason to use
a C style array is with a static array of POD types, to ensure
static initialization (and thus avoid any issues of order of
initialization).
new[] has to support the quantity not being
known at compile-time, so there's no way it
can return a pointer to array (whose size
must be known at compile time, otherwise you
couldn't assign it to anything).

Given the definition of C style arrays, agreed. new[] does the
best it can, given this definition. But this definition is
completely broken.
 
J

Juha Nieminen

Markus said:
I can see one reason to use it for scalar/POD types in certain
situations: To avoid the cost of initialization that std::vector has.

Excuse me? If you do a "new A[10]" then A's constructor *will* be
called 10 times. It's in on way different to what a std::vector does.
 
M

Martijn van Buul

* James Kanze:
In practice, it doesn't matter; in well over 15 years of C++, I've never,
ever found a case where you might want to do a new A[N], or use array new in
general.

That's what over-exposure to the STL does to you. It makes you think
std::vector<> is a one-stop cureall.
 
R

Richard Herring

Juha Nieminen said:
Markus said:
I can see one reason to use it for scalar/POD types in certain
situations: To avoid the cost of initialization that std::vector has.

Excuse me? If you do a "new A[10]" then A's constructor *will* be
called 10 times.

Even if A is int?
It's in on way different to what a std::vector does.

std::vector<A>(10) copies A() into each element, probably via
uninitialized_fill_n.
 
J

James Kanze

* James Kanze:
In practice, it doesn't matter; in well over 15 years of C++, I've never,
ever found a case where you might want to do a new A[N], or use array new in
general.
That's what over-exposure to the STL does to you. It makes you think
std::vector<> is a one-stop cureall.

Actually... I wasn't using std::vector<> very much 15 years ago.
In fact, I'd used C++ for well over 5 years before using my
first template. I've just never found a problem for which new
A[N] would be an appropriate answer. Even back in the days
before templates and exceptions.
 
M

Martijn van Buul

* James Kanze:
Actually... I wasn't using std::vector<> very much 15 years ago.
In fact, I'd used C++ for well over 5 years before using my
first template. I've just never found a problem for which new
A[N] would be an appropriate answer. Even back in the days
before templates and exceptions.

If you're not using stl::vector<>, and you're not using array new, how can
you ever create an array of items?
 
M

Markus Schoder

Markus said:
I can see one reason to use it for scalar/POD types in certain
situations: To avoid the cost of initialization that std::vector has.

Excuse me? If you do a "new A[10]" then A's constructor *will* be
called 10 times. It's in on way different to what a std::vector does.

Only if A is a non-POD class type (5.3.4/15) -- which I have excluded.
 
J

James Kanze

* James Kanze:
Actually... I wasn't using std::vector<> very much 15 years ago.
In fact, I'd used C++ for well over 5 years before using my
first template. I've just never found a problem for which new
A[N] would be an appropriate answer. Even back in the days
before templates and exceptions.
If you're not using stl::vector<>, and you're not using array new, how can
you ever create an array of items?

I wrote my own array classes, obviously.

new A[10] suffers from two major drawbacks. The first is common
to all use of new, you need to call delete sometimes, so you'll
normally need to wrap it in some kind of class with a
destructor. (An A[10] is not an entity object, which can
manage its own lifetime, so it needs specific lifetime
management by the user.) The second is more specific: it
constructs all 10 objects immediately, using the default
constructor. So any array class will not use it internally, but
call the operator new() function directly, to obtain raw memory,
then construct each of the elements separately (using placement
new to "call" the constructor at the desired address).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,495
Members
48,185
Latest member
abhaysingh01

Latest Threads

Top