Structure initialization using new - c++

Hello,

Declaring a structure in C++ results in automatic initialization which results in safe behavior even if you dont explicitly initialize everything in structure by yourself. For example, for a structure, containing pointers:
1
2
3
4
5
6
7
8
struct Demo
{
type* d1;
int *d2;
double d;
int x[4];
...
};

So, a structure is implicitly initialized to zero e.g. in C when declare a struct object as a global variable. Somebody told me that it does the same for calloc but (maybe) not for malloc. What about creating struct using new operator in C++, for example?

Demo *p_demo=new Demo();

Will it implicitly initialize the structure members to zero?

Thanks.


No.

Demo's default constructor runs the default constructor of all the elements. The default constructor for pointer-to-type does nothing; the default constructor for double and int and all other POD types also does nothing.
So, a structure is implicitly initialized to zero
Not really.

e.g. in C when declare a struct object as a global variable
C globals and static are initialised because of where they're placed (in a data segment), not because they're structs. So if used one as an auto variable (on the stack), it wouldn't be initialised.

Demo *p_demo=new Demo();
Will it implicitly initialize the structure members to zero?
Yes, for different reasons.
Here is demonstration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

struct Demo
{
  int x;
};

int main()
{
  Demo demo;

  demo.x = 5;

  Demo *p_demo = &demo;

  new(p_demo) Demo;

  cout << p_demo->x << endl; //prints 5

  new(p_demo) Demo();

  cout << p_demo->x << endl; //prints 0
}

I use placement new. I can not make the demonstration otherwise, because it seems that the free store (the part of the heap that new uses) is automatically zero-initialized by the OS or by the run-time and it appears as if you always have zero initialized structs. Placement new has the syntax new(ptr) Type(args) and has the effect of invoking the constructor of Type with the supplied args on the memory pointed by ptr. It is like the ordinary new, but does not allocate memory. It uses the memory that you provide with ptr instead. I use this form of new, because the allocation is not related to your question, only the initialization. And this helps me to show the effect I want to demonstrate. As a side note, what I do, to construct one object where another already resides, is generally illegal, but since this is POD type (plain old data - C like struct) I believe it is ok.

The first constructor - without parenthesis - provides what is called default initialization (do not confuse with default constructor). Default initialization leaves the values of fundamental types (int, double, etc) uninitialized, i.e. arbitrary. Therefore, with new Demo; you get uninitialized chunk of memory with arbitrary values in the fields.

The second constructor - with parenthesis - provides what is called value initialization. Value initialization zeros fundamental types. Therefore, with new Demo(); you get zero-initialized chunk of memory - all fields in this case will be zeros.

In general, the rules are defined recursively, and you may need to think deep to anticipate what will happen. I address your case. Specifically, you provide no constructor for Demo. The trick with leaving arbitrary values with default initialization (which is supposed to provide performance gain if you believe it) works only when the struct/class does not have constructor defined by the programmer.

The same techniques apply to automatic variables, but I rather say no more. My answer is already too bloated.

Regards
Topic archived. No new replies allowed.