When it comes to C++ initialization, we all know that we assign an initial value to a variable when it is defined. Indeed, initialization at every definition not only avoids dirty data but also increases the readability of the code. But do you know how many pitfalls there are?

Trap 1: pits initialized by default

If you look at the code, can you tell which variables have certain values?

int a;

void func()

{

int b;

static int c;

cout<<“a: “<<a<<“, b :”<<b<<“, c :”<<c<<endl;

}

int main()

{

cout<<“main a: “<<a<<endl;

func();

return 0;

}

The printed result is:

main a: 0

a: 0, b :32694, c :0

As you can see, none of the three variables are explicitly initialized, but a and c are given default values, while b is an indeterminate value.

The answer can be found in the C++Primer: “variables defined outside of any function body are initialized to 0. Variables of built-in types defined inside the function body are not initialized.” Variables defined outside the function body are global variables.

The compiler saves the initialized global variable in the data segment of the static storage area, and the more such values, the larger the program, the operating system will start the program, the value of the global variable will be copied to the data segment, that is, to complete the initialization of the variable.

Uninitialized global variables are stored by the compiler in the BSS segment of the static storage area, and such values do not make the program larger. The operating system allocates the corresponding memory when loading the program, and the BASS segment is cleared to 0, that is, the initialization of the variable is complete.

Therefore, variables defined outside the function body are assigned default values.

But variables defined in functions are memory allocated on the stack, which is a dynamic storage area. The operating system does not help you clear zeros in this area, so the values defined here are undefined.

Trap 2: Array initialization pit

int buff[10] = {0};

for(int i = 0; i<10; i++)

{

cout<<buff[i]<<endl;

}

So this is code that I’m sure you write all the time, and the answer is obviously 0, but is it really because {0} means you want to set each value to 0?

The answer is no, as shown in the following code:

int buff_2[10] = {1};

for(int i = 0; i<10; i++)

{

cout<<buff_2[i]<<“,”;

}

The answer is: 1,0,0,0,0,0,0,0,0,0, 0,0,0. Did you notice that? Only the first value is 1, and then all the zeros!

So, this pit is actually a pit for C++ initializer lists. The definition of initializer lists states that if the number of initializer lists is smaller than the defined number, the values that are not defined will be given default values.

Trap 3: The pit of memset

char buff[10];

memset(buff,0,sizeof(buff));

for(int i = 0; i<10; i++)

{

printf(“%d “,buff[i]);

}

You might think this code is pretty simple, and yes, it prints all zeros, which is pretty simple, but look at the following code:

int buff_2[10];

Memset (buff_2, 1, sizeof (buff_2));

for(int i = 0; i<10; i++)

{

printf(“%d “,buff_2[i]);

}

Yes, the type of the variable has changed, and the printed result is:

16843009 16843009 16843009 16843009 16843009

Don’t you think it’s strange, then, we put this sentence:

printf(“%d “,buff_2[i]);

To:

printf(“0X%x “,buff_2[i]);

The result is:

0X1010101 0X1010101 0X1010101 0X1010101 0X1010101 0X1010101 0X1010101 0X1010101 0X1010101 0X1010101

Does it feel weird?

On my device, int is 4 bytes, so buff_2 is 40 bytes in total, and memset assigns 0X01 to 40 1 bytes instead of 10 4 bytes. So don’t use memset to initialize non-character arrays!

Finally, if you want to improve your programming skills, learn C/C++ programming knowledge! Then you are in luck ~ “click the link” to join C/C++ penguin circle, here are some interesting things you may not know.