Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>First of all is this valid C++ or is it a microsoft extension?</em></p> <p>It is valid in C++, the relevant part of the standard is 5.3.4, with the first paragraph containing the grammar</p> <p><em>Is it guaranteed to initialize all the elements of the array?</em></p> <p>Yes. Paragraph 5.3.4/15 states that </p> <blockquote> <p>A new-expression that creates an object of type T initializes that object as follows:</p> <p>...</p> <ul> <li>If the new-initializer is of the form (), the item is value-initialized (8.5)</li> </ul> </blockquote> <p>where value initialized for POD means zero-initialize.</p> <p><em>Also, is there any difference if I do new int; or new int();? Does the latter guarantee to initialize the variable?</em></p> <p>Yes they are different. According with the quote above <code>new int()</code> will zero-initialize the integer. In a previous block of the same paragraph:</p> <blockquote> <p>If the new-initializer is omitted:</p> <ul> <li><p>If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized (8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.</p></li> <li><p>Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;</p></li> </ul> </blockquote> <p>so <code>new int</code> will not initialize the memory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload