0
class Example{

public:
int k;
};

int main(){
Example *ex = new Example();
Example *ex1 = new Example;
}

From what I have read so far, in the case of ex variable k will be value initialized, that means equals 0, and ex1 will be default initialized, and for basic types as int that means undefined behavior, but not 0. The problem is, when I print them

cout << ex->k << endl;
cout <<  ex1->k << endl;

it prints 0 for both. Why?

EDIT: According to C++ Standard - To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— 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 T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

The things look a little bit different to me now, in the case of ex1 we have default initialization, it's a POD class and not an array so , the object is zero-initialized, then - if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized; and that means that k will be initialized to 0 and it is not undefined behavior. Right ?

loannnlo
  • 111
  • 1
  • 6
  • 3
    Its still undefined behavior, printing `0` is just one variant of it. – Hatted Rooster Jul 24 '18 at 10:03
  • 2
    `*ex1` has an *indeterminate value* (not undefined behavior in itself, but possibly causing UB). That means that the value can be anything. Including that it can be 0. Since it's not `unsigned char` another possibility for the value is a trap representation, that could cause something undesired to happen when it's accessed. Or it could be totally random, or a compiler-dependent special bitpattern used to fill uninitialized memory. – Cheers and hth. - Alf Jul 24 '18 at 10:06
  • `From what I have read so far, in the case of ex variable k will be value initialized` Could you point out where you read this? – Killzone Kid Jul 24 '18 at 10:10
  • @KillzoneKid First answer here - https://stackoverflow.com/questions/8106016/c-default-initialization-and-value-initialization-which-is-which-which-is-ca – loannnlo Jul 24 '18 at 10:23
  • @loannnlo Thank you – Killzone Kid Jul 24 '18 at 10:56
  • @Cheersandhth.-Alf Can you check the edit and tell if I am wrong – loannnlo Jul 24 '18 at 13:37
  • @loannnlo: I've checked with the various versions of the standard, and for C++03 you would be right: `new T` gives a default-initialized object, and in C++03 for your POD type that means zero-initialized. Where the C++03 standard talks about POD, C++17 instead says this: “If T is a (possibly cv-qualified) class type (Clause 12), constructors are considered. The applicable constructors are enumerated (16.3.1.3), and the best one for the initializer () is chosen through overload resolution (16.3). The constructor thus selected is called, with an empty argument list, to initialize the object.” – Cheers and hth. - Alf Jul 24 '18 at 19:27
  • It helps to know that the automatically generated default constructor, in your case called by the default initialization, doesn't do anything about POD members. It just ignores them. But it does call constructors of class type members. – Cheers and hth. - Alf Jul 24 '18 at 19:31

1 Answers1

1

"Undefined behavior" means the compiler can do anything .Which is to say, that when you hit undefined behavior, the compiler would be completely within its rights to make demons fly out of your nose nasal demons.Thankfully,in your case it chose to print 0.

SynAck
  • 427
  • 5
  • 19
  • It's actually defined, but indeterminate. Which doesn't permit the nasal demons, but does allow the value 0. I'd give half an upvote if I could. – Toby Speight Jul 24 '18 at 14:55