动态铸造失败的原因

时间:2017-05-03 10:50:18

标签: c++ dynamic-cast heap-corruption

动态强制转换在其工作的同一功能中失败。 动态强制转换在调用commitTransaction之前正在工作,并在commitTransaction之后失败。 在commitTransaction中,调用copy运算符,执行delete和new操作。有什么想法在这里发生了什么?我读到堆损坏会导致dynamic_cast失败。任何人都可以解释为什么会这样吗?

#include <iostream>
#include <string>

using namespace std;
#define num 10000
#define numOfWords 10000
class RollbackAllocator                                                                           
{    
    public:
    int*  memBitMap;
    RollbackAllocator & operator=(const RollbackAllocator& rollBack)                                              
    {                                                                                                             
        delete[] memBitMap;                                                                                       

        if(rollBack.memBitMap)                                                                                    
        {                                                                                                         
            memBitMap = new int[num];                                                                          
            for(int i = 0;i<num;i++)                                                                           
            {                                                                                                     
                memBitMap[i] = rollBack.memBitMap[i];                                                             
            }                               
        }                                                                                                         
        else                                                                                                      
        {                                                                                                         
            memBitMap = 0;                                                                                        
        }                                                                                                         
        return *this;                                                                                             
    };
    void init()
    {
        memBitMap = new int[num];
    }

};

class ResourceAllocator
{
    public:
    int temp;
    virtual int tempMethod(){
        temp = 0;
        };
};

class KatanaResourceAllocator : public ResourceAllocator
{
    public:
    RollbackAllocator   mIFPBPDUEntryIndexAllocatorNM;
    RollbackAllocator   mIFPBPDUEntryIndexAllocatorNMShadow;
    void commitTransaction()
    {
        mIFPBPDUEntryIndexAllocatorNMShadow = mIFPBPDUEntryIndexAllocatorNM;
    }
};

int main() {

ResourceAllocator * resourceAllocator = new KatanaResourceAllocator();
    KatanaResourceAllocator * allocator = dynamic_cast<KatanaResourceAllocator *>(resourceAllocator); //dynamic_cast works fine type().name= P17ResourceAllocator
    cout<< " pre allocator=" << allocator;
    allocator->mIFPBPDUEntryIndexAllocatorNM.init();
    allocator->mIFPBPDUEntryIndexAllocatorNMShadow.init();
    allocator->commitTransaction();
    allocator = dynamic_cast<KatanaResourceAllocator *>(resourceAllocator); //allocator is null here type().name= P17ResourceAllocator
    cout<<" post allocator=" << allocator;

    return 0;
}

1 个答案:

答案 0 :(得分:-1)

此代码通过复制未初始化的int值导致未定义的行为。特别是这一行:

 memBitMap[i] = rollBack.memBitMap[i];  

rollBack.memBitMap[i]表示由int创建的new int[num];,它不会初始化值。

相关问题