gpt4 book ai didi

c++ - 使用 memcpy 时出现内存错误?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:45 25 4
gpt4 key购买 nike

我正在使用 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
}

最佳答案

fullBuffer 的声明更改为:

Uint8 * fullBuffer = new Uint8[int(sizeF*numOfFrames)];

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

关于c++ - 使用 memcpy 时出现内存错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com