这是正常的 c++ 行为吗?编译器零初始化我的类,尽管有一个用户定义的构造函数
Is this normal c++ behavior? compiler zero initialize my class despite having a user defined constructor
让我描述一下我的问题。我已经分析了使用 OllyDbg 使用 Visual Studio 2015 [发布] 生成的以下代码,并且调用了两次 memset(一次为 408 字节,后者为 400 字节)。显然,408字节是编译器对我的类进行零初始化。
为什么会这样?
我的主类已经有一个构造函数,它应该摆脱编译器生成的构造函数。
有趣的是:如果我从主类中删除成员"someotherclas sb",m_buffer只会设置一次(由我设置,有 400 个字节)。
// ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class someotherclas
{
public:
int * ptr;
};
class mainclass
{
public:
char m_buffer[400];
someotherclas sb; //If I remove this, buffer will be memset once only (by me, in the constructor)
int a;
mainclass()
{
memset(m_buffer, 0x00, sizeof(m_buffer));
a = 6;
}
};
int main(int argc, char * arr[])
{
mainclass * buffer2 = new mainclass;
return 0;
}
我想发生这种情况是因为为您的项目启用了安全开发生命周期检查。你能检查编译器选项吗?如果是这样,则适用以下规定:
启用/sdl 后,编译器...执行类成员 初始化。自动将所有类成员初始化为零 对象实例化(在构造函数运行之前)。这有助于防止 使用与类成员关联的未初始化数据 构造函数不显式初始化。
相关文章:
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么我的最后一个 ELSE 条件无法正确执行
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 编译器说这个函数必须返回一个值,但我已经有一个 return 语句
- 在我的第一个C++程序中需要一些帮助(简单)
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 我可以有一个与另一个向量共享存储的向量
- 我可以有一个从外部不可见但未在标头中定义的静态友元函数吗?
- 我正在尝试在Visual Studio 2013中编写我的第一个"Hello World"代码。为什么我会收到"IntelliSense: no operator message"和"error C2
- 我可以有一个模板函数指针的 std::vector 吗?
- 为什么海湾合作委员会声称我违反了"at least one argument for variadic macro",而我至少有一个论点?
- 我可以有一个仅使用工厂方法创建的成员变量吗?
- 在c++中,我可以有一个只有在特定条件下才具有某些函数的类吗
- 我可以有一个要实例化的模板列表,而无需事先实例化
- 我可以有一个异常的静态全局实例吗?
- 我可以有一个跳过索引的布尔值的单维数组吗?
- 我可以有一个指向C++中函数的静态指针数组吗
- 我如何有一个平台相关的检查,而没有-Wtype-limits警告
- 我的第一个DLL有很多我不明白的编译错误
- 我可以有一个返回*this并处理非常量对象的const成员函数吗