Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value initialization and Non POD types

An hour ago I posted an answer here which according to me was correct. However my answer was downvoted by Martin B. He said

You're just lucky and are getting zeros because the memory that i was placed in happened to be zero-initialized. This is not guaranteed by the standard.

However after reading Michael Burr's answer here and trying the following sample code

1)

#include <cassert>

struct B { ~B(); int m; };

int main()
{
   B * b = new B();
   assert(b->m == 0);
}

I got a debug error on MSVC++ 2010.

I got a similar error when I tried the following code [My answer here] on MSVC++2010

2)

#include <cassert>
struct Struct {
    std::string String;
    int Int;
    bool k;
    // add add add
};

struct InStruct : Struct
{
   InStruct() : Struct() {}
};

int main()
{
   InStruct i;
   assert(i.k == 0);
}

Neither (1) nor (2) gave any such error on gcc/Clang which made me think if MSVC++2010 does not support C++03. I am not sure.

According to Michael Burr's post [in C++03]

new B() - value-initializes B which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.

The Standard says

To value-initialize an object of type Tmeans:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if Thas no accessible default constructor);

.....

otherwise, the object is zero-initialized

From the first point if there is no user declared default constructor the compiler synthesized default constructor will be called which will zero initialize all the fields (according to last point).

So where am I wrong? Is my interpretation of value initialization correct?

like image 842
Prasoon Saurav Avatar asked Oct 14 '10 08:10

Prasoon Saurav


People also ask

What is POD and non POD?

A POD (plain old data) object has one of these data types--a fundamental type, pointer, union, struct, array, or class--with no constructor. Conversely, a non-POD object is one for which a constructor exists.

What is POD data type?

In computer science and object-oriented programming, a passive data structure (PDS, also termed a plain old data structure, or plain old data, POD) is a term for a record, to contrast with objects.

What is POD Type C++?

POD is an acronym in C++ that means plain old data. It is a class/struct that ONLY has member variables and no methods, constructors, destructors, virtual functions, etc.


2 Answers

Visual Studio has known bugs in all current versions (2005, 2008, 2010) where it doesn't correctly implement value-initialization for non-POD types that don't have a user declared constructor.

By the language rules none of you asserts should fire but do exhibit the compiler issues. These are some of the bug reports, note that they are all closed or resolved as "Won't Fix".

http://connect.microsoft.com/VisualStudio/feedback/details/564268/c-value-initialization

http://connect.microsoft.com/VisualStudio/feedback/details/484295/vc-does-not-value-initialize-members-of-derived-classes-without-user-declared-constructor

http://connect.microsoft.com/VisualStudio/feedback/details/100744/value-initialization-in-new-expression

like image 166
CB Bailey Avatar answered Oct 19 '22 00:10

CB Bailey


For people who stumble upon this question in 2015, like me:

All of the above issues have been fixed in VS 2015. Value initialization now works as defined in the standard.

like image 25
CodeMonkey Avatar answered Oct 19 '22 01:10

CodeMonkey