Why it works at Release, but in Debug, it can't run sucessfully



  • void Reconstruct::allocation()
    {
        //向系统申请分配指定size个字节的内存空间
        int tempSize = sizeof(double)*reparam.ParamsnX*reparam.ParamsnY*reparam.ParamsnZ;
        ImageOutSave = (double***)malloc(tempSize);
        //if (ImageOutSave == NULL) qDebug() << "ImageOutSave Null";
        for(unsigned int i = 0; i < reparam.ParamsnX; ++i)
        {
            int tempSizeX = sizeof(double)*reparam.ParamsnY*reparam.ParamsnZ;
            ImageOutSave[i] = (double**)malloc(tempSizeX);
            //if (ImageOutSave[i] == NULL) qDebug() << "ImageOutSave[" << i << "] Null";
            for(unsigned int j = 0; j < reparam.ParamsnY; ++j)
            {
                ImageOutSave[i][j] = (double*)malloc(sizeof(double)*reparam.ParamsnZ);
    
                for(unsigned int k = 0; k < reparam.ParamsnZ; ++k)
                    ImageOutSave[i][j][k]=0;
            }
        }
    }
    


  • @Solan If I wanted to be evil I would answer "because you write C, not C++". But if you want to get answers, you should give information about your system. At least Qt version, compiler name and version, hardware (processor).


  • Moderators

    @Solan To add to @Eeli-K : why don't you say what is not working?
    Why do you use malloc and C style casts?



  • You are allocating WAY too much memory, just treat 1 dimension at a time, do not multiply the dimensions together from the start:

    double*** ImageOutSave=nullptr;
    ImageOutSave = new double**[reparam.ParamsnX];
     for(unsigned int i = 0; i < reparam.ParamsnX; ++i){
    ImageOutSave[i] = new double*[reparam.ParamsnY];
    for(unsigned int j = 0; j < reparam.ParamsnY; ++j)
            {
    ImageOutSave[i][j] = new double [reparam.ParamsnZ];
    for(unsigned int k = 0; k < reparam.ParamsnZ; ++k)
                    ImageOutSave[i][j][k]=0;
    }
    }
    
    

    There are easier ways to manage this kind of containers though for example using boost multi array



  • @Solan
    It will not work in debug mode because in Debug mode there some extra initializations that happen behind the scenes. If you heavily use the pointers the visual debuggers won't help you out.
    For more information on this topic read about -Wall option in gcc for linux and go to msdn about debugger from microsoft windows.
    If the code you are writing from scratch then please use latest conventions of new and delete.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.