构造函数中的非零默认值

发布于 2024-11-09 18:47:14 字数 482 浏览 7 评论 0原文

可能的重复:
类中未定义/未初始化的默认值

我有一个 C++ 类它的创建和销毁相当频繁。为了进行调试,我在类中添加了一个 bool mDebug,我想用 #ifdef 打开它。但是,我发现即使未定义 ifdef,也会出现与调试变量相关的消息。当我转储构造函数中的值时,我发现它具有随机值 - 35、68 等。因此,我假设存在某种内存泄漏,并且该类正在分配已在使用中的内存。但是,我已经对其运行了 purify,并且它没有显示此时未初始化的内存读取或任何越界写入。 valgrind 也没有太大帮助。由于多种原因,gdb 在此代码上运行得不太好。我可以转储变量的地址,但是还有其他想法吗?

谢谢

Possible Duplicate:
Undefined / Uninitialized default values in a class

I have a class in C++ that is created and destroyed fairly often. For debugging, I've added a bool mDebug to the class, that I want to turn on with an #ifdef. However, I discovered the messages associated with the debug variable appearing even when the ifdef isn't defined. When I dumped the value in the constructor, I found that it has random values - 35, 68, etc. So I assume that there's some kind of memory leak, and the class is being allocated memory that's already in use. However, I've run purify on it, and it's not showing an uninitialized memory read at that point or any writes out of bounds. valgrind also hasn't been too helpful. For a variety of reasons, gdb doesn't work so well on this code. I can dump the address of the variable, but any other ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-11-16 18:47:14

我假设添加到类中的“bool mDebug”不是静态成员,因为它会(自动)初始化为零。所以,我认为它一定是一个“常规”数据成员。

这意味着您根本没有使用“基/成员初始值设定项列表”或在构造函数主体中显式设置其初始值(您也可以将其包装在 #ifdef 中,因为您的成员定义是包含在 #ifdef 中)。

默认情况下,成员未设置为零(您必须明确执行此操作)。除非发生了非常奇怪的事情,否则我怀疑这是内存泄漏/错误。

I'm assuming your "bool mDebug" added to the class is not a static member, because that would (automatically) be initialized to zero. So, I assume it must be a "regular" data member.

That means you simply did not set its initial value with a "base/member initializer list" or explicitly in the constructor body (either of which you would also wrap in an #ifdef, since your member definition is wrapped in an #ifdef).

Members are not set to zero by default (you must do that explicitly). Unless something really strange is going on, I doubt it is a memory leak/error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文