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

P

peter.xiau

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?
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
S

Simon Biber

Alf said:
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"
 
O

Old Wolf

Alf said:
* (e-mail address removed):
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..
 
A

Andrey Tarasevich

...
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.
 
S

Sumit Rajan

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.
 
A

Andrey Tarasevich

Sumit said:
...

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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top