构造函数中的字段初始化会损坏内存
Field initialization in constructor corrupts memory
出于某种原因,来自 UNIT 测试的构造函数中的字段初始化会损坏内存。
我有以下课程
//.h
class Entity
{
public:
...
Entity();
private:
unsigned int _nextOperatorId;
unsigned int _operators[30][4]; //from consts
...
}
//.cpp
Entity::Entity() : _operators(), _nextOperatorId(1)
{
/* If i run this from unit test i see:
_operators [0] 0x0569bb38 {3452816845, 3452816845, 3452816845, 3452816845, 1}
_operators [1] 0x0569bb4c {3452816845, 0, 0, 0, 0}
_operators [2] 0x0569bb4c {0, 0, 0, 0, 0}
... (all other rows are zeroes).
If i delete _nextOperatorId(1) initialiazation, or if i run constructor from console app, here all as expected - all rows in operators array are zeroes * /
}
我在 VS 单元测试类初始值设定项中运行它,如下所示:
private
Entity* entity;
public:
TEST_METHOD_INITIALIZE(ClassInitialize)
{
entity = new Entity();
}
那么为什么我加_nextOperatorId(1(后内存会变坏呢?一切看起来都那么简单..
VC++编译器中有一个已知的错误;除非在你的版本中修复了它,否则不幸的是,你不能依靠VC++来按照C++标准的要求对类成员进行值初始化。
http://connect.microsoft.com/VisualStudio/feedback/details/564268/c-value-initialization
https://connect.microsoft.com/VisualStudio/feedback/details/746973/incorrect-c-11-value-initialization-for-type-with-implicitly-declared-but-non-trivial-default-constructor
相关文章:
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 正在调试 malloc():新内存损坏
- 仅特定内核计数上的 MPI 内存损坏
- C++程序错误:malloc():内存损坏
- 内存地址是否由于未对齐的内存地址而损坏?
- 调用std::函数成员时内存损坏
- 通过分配大量内存来跟踪堆损坏
- C++ 内存损坏检测
- 由于标头中的 #define 不匹配而导致内存损坏
- C STD ::线程中的种族状况或内存损坏
- malloc()内存损坏仅通过为特定数量编写int数组
- 字符串标记化期间的内存损坏
- 从检测到 glibc 正常退出 - malloc():内存损坏
- 放置在外部 DLL 中的类中的字符串数据的内存损坏
- std :: string ::擦除会导致内存损坏
- 编译器如何检测内存损坏
- 获得MAC地址的代码似乎会损坏内存,但仅在发布构建中而不是调试构建
- Visual Studio在使用sockaddr_storage时会损坏内存
- 构造函数中的字段初始化会损坏内存
- 字符串结束字符会损坏内存吗?缓冲区[大小]='\0';