Copying structs with uninitialized members

后端 未结 4 1771
迷失自我
迷失自我 2020-12-30 20:47

Is it valid to copy a struct some of whose members are not initialized?

I suspect it is undefined behavior, but if so, it makes leaving any uninitialized members in

4条回答
  •  猫巷女王i
    2020-12-30 21:20

    Since all members of the Data are of primitive types, data2 will get exact "bit-by-bit copy" of the all members of data. So the value of data2.b will be exactly the same as value of the data.b. However, exact value of the data.b cannot be predicted, because you have not initialized it explicitly. It will depend on values of the bytes in the memory region allocated for the data.

提交回复
热议问题