Proper way to initialize C++ structs

后端 未结 6 534
误落风尘
误落风尘 2020-12-07 08:11

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginn

6条回答
  •  再見小時候
    2020-12-07 09:05

    From what you've told us it does appear to be a false positive in valgrind. The new syntax with () should value-initialize the object, assuming it is POD.

    Is it possible that some subpart of your struct isn't actually POD and that's preventing the expected initialization? Are you able to simplify your code into a postable example that still flags the valgrind error?

    Alternately perhaps your compiler doesn't actually value-initialize POD structures.

    In any case probably the simplest solution is to write constructor(s) as needed for the struct/subparts.

提交回复
热议问题