初始化 C++ 的正确方法结构体
我们的代码涉及一个 POD(普通旧数据结构)结构(它是一个基本的 C++ 结构,其中包含其他结构和 POD 变量,需要在一开始就进行初始化。)
基于我的 阅读,似乎:
myStruct = (MyStruct*)calloc(1, sizeof(MyStruct));
应该将所有值初始化为零,如下所示:
myStruct = new MyStruct();
但是,当在中初始化结构时第二种方式,当使用这些变量时,Valgrind 后来抱怨“条件跳转或移动取决于未初始化的值”。我的理解是否有缺陷,或者 Valgrind 是否抛出误报?
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 beginning.)
Based one what I've read, it seems that:
myStruct = (MyStruct*)calloc(1, sizeof(MyStruct));
should initialize all the values to zero, as does:
myStruct = new MyStruct();
However, when the struct is initialized in the second way, Valgrind later complains "conditional jump or move depends on uninitialised value(s)" when those variables are used. Is my understanding flawed here, or is Valgrind throwing false positives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 C++ 中,类/结构是相同的(就初始化而言)。
非 POD 结构也可以有一个构造函数,以便它可以初始化成员。
如果您的结构是 POD,那么您可以使用初始值设定项。
或者,您可以使用默认构造函数。
我相信 valgrind 正在抱怨,因为这就是 C++ 过去的工作方式。 (我不太确定 C++ 何时使用零初始化默认构造进行升级)。最好的选择是添加一个初始化对象的构造函数(结构体是允许的构造函数)。
附注:
许多初学者都尝试重视 init:
快速搜索“Most Veshing Parse”将提供比我更好的解释。
In C++ classes/structs are identical (in terms of initialization).
A non POD struct may as well have a constructor so it can initialize members.
If your struct is a POD then you can use an initializer.
Alternatively you can use the default constructor.
I believe valgrind is complaining because that is how C++ used to work. (I am not exactly sure when C++ was upgraded with the zero initialization default construction). Your best bet is to add a constructor that initializes the object (structs are allowed constructors).
As a side note:
A lot of beginners try to value init:
A quick search for the "Most Vexing Parse" will provide a better explanation than I can.
我写了一些测试代码:
g++编译并运行:
I write some test code:
g++ compile and run:
根据您告诉我们的情况,这似乎是 valgrind 中的误报。带有
()
的new
语法应该对对象进行值初始化(假设它是 POD)。您的结构的某些子部分是否可能实际上不是 POD,这会阻止预期的初始化?您是否能够将代码简化为仍标记 valgrind 错误的可发布示例?
或者,您的编译器可能实际上并未对 POD 结构进行值初始化。
在任何情况下,最简单的解决方案可能是根据结构/子部分的需要编写构造函数。
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.
由于它是一个 POD 结构,因此您始终可以将其 memset 为 0 - 这可能是初始化字段的最简单方法(假设这是适当的)。
Since it's a POD struct, you could always memset it to 0 - this might be the easiest way to get the fields initialized (assuming that is appropriate).
在我看来,这是最简单的方法。结构成员可以使用花括号“{}”进行初始化。例如,以下是有效的初始化。
关于 C++ 中的结构有很好的信息 - https://www.geeksforgeeks.org/structurals-在cpp中/
That seems to me the easiest way. Structure members can be initialized using curly braces ‘{}’. For example, following is a valid initialization.
There is good information about structs in c++ - https://www.geeksforgeeks.org/structures-in-cpp/
您需要初始化结构中的任何成员,例如:
You need to initialize whatever members you have in your struct, e.g.: