473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(us ing 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
6 4385
* pe********@gmai l.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
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
Alf P. Steinbach wrote:
* pe********@gmai l.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
pe********@gmai l.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

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:11******** *****@news.supe rnews.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.hc ltech.com>
Nov 22 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3538
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
18
8039
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller objects, again allocated using "new". From my tests I deduce that a considerable part of the computational time is spent on the memory allocation, which...
8
1402
by: Maarten | last post by:
Hello, I'm a software engineer for a Dutch company and I am working on the improvement of a already made program and I've found a very curious problem. It consists of two parts, explained below: When one of my functions is called, the program works as it is supposed to do, but when I add a line, say for example:
7
1322
by: Jamie Julius | last post by:
Consider the following struct: struct TestStruct { public int a, b, c; public TestStruct(int a, int b, int c) { this.a = a;
3
2263
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
14
5665
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it places on application code. Take, for instance, this C++ construct: class foo { char *m_name;
6
2695
by: Warly girl | last post by:
Hi i want your help again in c++ !! i want a function change the "int" to "string" because in my project "Registration system" in task "b" i must write a course class which contains number of credits and a string id. In addition to the constructor, it should contain a print function.. and the id of course is seprate in 2 part "for...
1
2138
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's part of the code: const int range = 1000000; int *array = new int(range); if (array == 0) cout<<"null ptr returned"<<endl;
350
11612
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.