473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what does a new A[10] return?

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
14 1317
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
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
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
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
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
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
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
* 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
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
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
* 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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1886
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
18
3105
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
9
2628
by: jim | last post by:
i want to make a c file that i can 'scanf ' students scores of 2 classes and their names , and i want it to get the sum of the 2 scores and make them in order .at last 'printf' /*am sorry,my...
2
2735
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
13
520
by: Protoman | last post by:
I'm getting an error: 10 C:\Dev-Cpp\Enigma.cpp no match for 'operator<' in 'i < (+cleartext)->std::basic_string<_CharT, _Traits, _Alloc>::end ()' Code: Enigma.hpp...
4
6672
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
15
8987
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
92
6118
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
2
1936
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
9
2413
by: zslevi | last post by:
I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continuations.html and I've found a strange usage of lambda: #################### Now, CPS would transform the baz function above...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.