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

"new int[10]" and "new int[10]()"

P: n/a
I found that
std::vector<int> v(10) ;
will automatically initiallize every elem to 0 in the vector,
I check the source code (VS.NET2003),
and I found a line of code like this
*T = new T[n]() ;

So I tested the three lines of code
1. int *p = new int[10] ;
2. int *p = new int[10]() ;
3. std::vector<int> v[10] ;

In Code::Blocks(using gcc), the result is:
1. Elements do NOT be initiallized.
2. Elements do NOT be initaillized.
3. Elements are initiallized.

In VS.NET 2003, the result is:
1. Elements do NOT be initiallized.
2. Elements are initiallized.
3. Elements are initiallized.

The result confusing me,
In the C++ standard,
Does "new int[10]()" need to initiallize the elements?
Or the behavior is depend on compilers?

Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* pe********@gmail.com:
In the C++ standard,
Does "new int[10]()" need to initiallize the elements?


Yes. One learns something new every day. I thought the syntax was a
language-extension, but when I looked it up it turns out it's standard.

Syntax in 5.3.4/1.

Semantics in 5.3.4/15.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #2

P: n/a
Alf P. Steinbach wrote:
Yes. One learns something new every day. I thought the syntax was a
language-extension, but when I looked it up it turns out it's standard.

Syntax in 5.3.4/1.

Semantics in 5.3.4/15.


If you're going to use non-ASCII characters in your posts, please
include a valid Content-type header. I assume that you meant U+00A7
SECTION SIGN, which means your post was probably ISO-8859-1. In that
case, it should have had the header
Content-Type: text/plain; charset="iso-8859-1"

--
Simon.
Nov 22 '05 #3

P: n/a
Alf P. Steinbach wrote:
* pe********@gmail.com:
In the C++ standard,
Does "new int[10]()" need to initiallize the elements?


Yes. One learns something new every day. I thought the syntax was
a language-extension, but when I looked it up it turns out it's
standard.


I thought it was an addition in the 2003 standard..

Nov 22 '05 #4

P: n/a
pe********@gmail.com wrote:
...
The result confusing me,
In the C++ standard,
Does "new int[10]()" need to initiallize the elements?
Yes. The elements have to be default-initialized, which is
zero-initialization for type 'int'
Or the behavior is depend on compilers?


The standard requires it. But many older compilers "forget" to perform
the initialization when the '()' initializer is used. This used to be a
popular bug among several compilers.

--
Best regards,
Andrey Tarasevich
Nov 22 '05 #5

P: n/a

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:11*************@news.supernews.com...
Yes. The elements have to be default-initialized, which is


Not that I can see the difference between a "value initialize" and "default
initialize" in this case, but doesn't the Standard (5.3.4/15) say "value
initialize" in this case?

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Nov 22 '05 #6

P: n/a
Sumit Rajan wrote:
...
Yes. The elements have to be default-initialized, which is


Not that I can see the difference between a "value initialize" and "default
initialize" in this case, but doesn't the Standard (5.3.4/15) say "value
initialize" in this case?
...


You are right. Value-initialization is one of the changes made by TC1. I
was just thinking in terms of the original C++98 standard.

--
Best regards,
Andrey Tarasevich

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.