What do the following phrases mean in C++: zero-, default- and value-initialization? -


what following phrases mean in c++:

  • zero-initialization,

  • default-initialization, and

  • value-initialization

what should c++ developer know them?

one thing realize 'value-initialization' new c++ 2003 standard - doesn't exist in original 1998 standard (i think might difference that's more clarification). see kirill v. lyadvinsky's answer definitions straight standard.

see previous answer behavior of operator new details on the different behavior of these type of initialization , when kick in (and when differ c++98 c++03):

the main point of answer is:

sometimes memory returned new operator initialized, , won't depending on whether type you're newing pod, or if it's class contains pod members , using compiler-generated default constructor.

  • in c++1998 there 2 types of initialization: 0 , default
  • in c++2003 3rd type of initialization, value initialization added.

to least, it's rather complex , when different methods kick in subtle.

one thing aware of msvc follows c++98 rules, in vs 2008 (vc 9 or cl.exe version 15.x).

the following snippet shows msvc , digital mars follow c++98 rules, while gcc 3.4.5 , comeau follow c++03 rules:

#include <stdio.h> #include <string.h> #include <new>  struct { int m; }; // pod struct b { ~b(); int m; }; // non-pod, compiler generated default ctor struct c { c() : m() {}; ~c(); int m; }; // non-pod, default-initialising m  int main() {     char buf[sizeof(b)];     memset( buf, 0x5a, sizeof( buf));      // use placement new on memset'ed buffer make sure      //  if see 0 result it's due explicit      //  value initialization     b* pb = new(buf) b();   //c++98 rules - pb->m uninitialized                             //c++03 rules - pb->m set 0     printf( "m  %d\n", pb->m);     return 0; } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -