使用memcpy时内存错误?

the*_*ive 5 c++ dicom

我正在使用dcmtk库来修改多帧压缩dicom图像的像素数据.因此,为此,在for循环的一个阶段,我取每个解压缩帧的像素数据并根据我的愿望修改它们,并尝试逐帧连接每个修改像素数据的大内存缓冲区.for循环的核心过程如下.

问题是在第一次迭代之后它在我调用函数的代码行中给出了内存getUncompressedFrame.我认为这是因为线路正在发生memcpy(fullBuffer+(i*sizeF),newBuffer,sizeF);,因为当我移除那条线时,那时没有错误,整个for循环工作得非常好.

如果我在使用memcpy时犯了错误,请你说出来吗?谢谢.

Uint32 sizeF=828072;// I just wrote it to show what is the data type. 
Uint8 * fullBuffer = new Uint8(int(sizeF*numOfFrames));//The big memory buffer
for(int i=0;i<numOfFrames;i++)
{
    Uint8 * buffer = new Uint8[int(sizeF)];//Buffer for each frame
    Uint8 * newBuffer = new Uint8[int(sizeF)];//Buffer in which the modified frame data is stored 
    DcmFileCache * cache=NULL;
    OFCondition cond=element->getUncompressedFrame(dataset,i,startFragment,buffer,sizeF,decompressedColorModel,cache);
    //I get the uncompressed individual frame pixel data 
    if(buffer != NULL)
    {
        for(unsigned long y = 0; y < rows; y++)
        {
            for(unsigned long x = 0; x < cols; x++)
            {
                if(planarConfiguration==0)
                {
                    if(x>xmin && x<xmax && y>ymin && y<ymax)
                    {
                        index=(x + y +  y*(cols-1))*samplePerPixel;
                        if(index<sizeF-2)
                        {
                            newBuffer[index]  = 0;
                            newBuffer[index + 1]  = 0;
                            newBuffer[index +2]  = 0;
                        }
                    }
                    else
                    {
                        index=(x + y +  y*(cols-1))*samplePerPixel;
                        if(index<sizeF-2)
                        {
                            newBuffer[index]  = buffer[index];
                            newBuffer[index + 1]  = buffer[index + 1];
                            newBuffer[index + 2]  = buffer[index + 2];
                        }
                    }
                }
            }
        }
        memcpy(fullBuffer+(i*sizeF),newBuffer,sizeF);
        //concatenate the modified frame by frame pixel data
    }                   
Run Code Online (Sandbox Code Playgroud)

kol*_*kol 10

将声明更改为fullBuffer:

Uint8 * fullBuffer = new Uint8[int(sizeF*numOfFrames)];
Run Code Online (Sandbox Code Playgroud)

您的代码没有分配数组,它分配了一个Uint8带有值的数组int(sizeF*numOfFrames).