When a class/struct members are initialized to zero during construction?

  • Thread starter Thread starter Manda Rajo
  • Start date Start date
M

Manda Rajo

Guest
Hi, it's very embarrassing that if "I don't use a pointer" (PtrOFF) and "I use a constructor" (ContructorON) with a class then the class members aren't initialized to zero, they are initialized to 0xcdcdcdcd..., the other cases are initialized to zero 0x00000000..., I don't understand the logic. Is there someone that knows a documentation about it? Or is it a bug on Visual Studio 2015?

I use this forum to archive my problem because it's difficult to remember the logic so I write everything here.

class Class__PtrOFF_ContructorOFF { public: int m_x, m_y; };
class Class__PtrON__ContructorOFF { public: void *m_ptr; int m_x, m_y; };
class Class__PtrOFF_ContructorON { public: int m_x, m_y; Class__PtrOFF_ContructorON() {} ~Class__PtrOFF_ContructorON() {} };
class Class__PtrON__ContructorON { public: void *m_ptr; int m_x, m_y; Class__PtrON__ContructorON() {} ~Class__PtrON__ContructorON() {} };
struct Struct_PtrOFF_ContructorOFF { public: int m_x, m_y; };
struct Struct_PtrON__ContructorOFF { public: void *m_ptr; int m_x, m_y; };
struct Struct_PtrOFF_ContructorON { public: int m_x, m_y; Struct_PtrOFF_ContructorON() {} ~Struct_PtrOFF_ContructorON() {} };
struct Struct_PtrON__ContructorON { public: void *m_ptr; int m_x, m_y; Struct_PtrON__ContructorON() {} ~Struct_PtrON__ContructorON() {} };

int main()
{
Class__PtrOFF_ContructorOFF *c00 = new Class__PtrOFF_ContructorOFF(); // 0x00000000...
Class__PtrON__ContructorOFF *c10 = new Class__PtrON__ContructorOFF(); // 0x00000000...
Class__PtrOFF_ContructorON *c01 = new Class__PtrOFF_ContructorON(); // 0xcdcdcdcd... (not initialized to zero)
Class__PtrON__ContructorON *c11 = new Class__PtrON__ContructorON(); // 0x00000000...

Struct_PtrOFF_ContructorOFF *s00 = new Struct_PtrOFF_ContructorOFF(); // 0x00000000...
Struct_PtrON__ContructorOFF *s10 = new Struct_PtrON__ContructorOFF(); // 0x00000000...
Struct_PtrOFF_ContructorON *s01 = new Struct_PtrOFF_ContructorON(); // 0xcdcdcdcd... (not initialized to zero)
Struct_PtrON__ContructorON *s11 = new Struct_PtrON__ContructorON(); // 0xcdcdcdcd... (not initialized to zero)
return 0;
}

Continue reading...
 
Back
Top