构造函数中的字段初始化会破坏内存

时间:2014-08-29 07:47:34

标签: c++ visual-studio unit-testing

对于来自UNIT测试的构造函数中的某些reasone字段初始化,会破坏内存。

我有以下课程

//.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)之后内存会被破坏?一切看起来都那么简单..

1 个答案:

答案 0 :(得分:2)