By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

what does a new A[10] return?

P: n/a
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

Jun 10 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Jess wrote:
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.
Jun 10 '07 #2

P: n/a
On Jun 10, 2:01 pm, Jess <w...@hotmail.comwrote:
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 []*
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

P: n/a
On Sun, 10 Jun 2007 21:18:47 +0000, James Kanze wrote:
On Jun 10, 2:01 pm, Jess <w...@hotmail.comwrote:
>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 []*

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.

--
Markus Schoder
Jun 10 '07 #4

P: n/a
On Jun 11, 9:18 am, James Kanze <james.ka...@gmail.comwrote:
>
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).
Jun 10 '07 #5

P: n/a
On Jun 11, 12:08 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Sun, 10 Jun 2007 21:18:47 +0000, James Kanze wrote:
[...]
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 11 '07 #6

P: n/a
On Jun 11, 12:17 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 11, 9:18 am, James Kanze <james.ka...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 11 '07 #7

P: n/a
Markus Schoder wrote:
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.
Jun 13 '07 #8

P: n/a
* 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.

--
Martijn van Buul - pi**@dohd.org
Jun 13 '07 #9

P: n/a
In message <46**********************@news.song.fi>, Juha Nieminen
<no****@thanks.invalidwrites
>Markus Schoder wrote:
>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.

--
Richard Herring
Jun 13 '07 #10

P: n/a
On Jun 13, 5:00 pm, Martijn van Buul <p...@dohd.orgwrote:
* 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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 13 '07 #11

P: n/a
* 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?

--
Martijn van Buul - pi**@dohd.org
Jun 14 '07 #12

P: n/a

Martijn van Buul <pi**@dohd.orgwrote in message...
>
If you're not using stl::vector<>, and you're not using array new, how can
you ever create an array of items?
int array[ 10 ];

<G>
--
Bob R
POVrookie
Jun 14 '07 #13

P: n/a
On Wed, 13 Jun 2007 15:01:12 +0300, Juha Nieminen wrote:
Markus Schoder wrote:
>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.

--
Markus Schoder
Jun 14 '07 #14

P: n/a
On Jun 14, 3:35 pm, Martijn van Buul <p...@dohd.orgwrote:
* 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).

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 15 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.