正在将指针指向的数据复制到另一个指针中

Copying data pointed at by pointer into another pointer

本文关键字:指针 复制 另一个 数据      更新时间:2023-10-16

我已经研究了一些类似的问题,但没有解决方案适用于我的案例。

我有一个类,它有一个连续运行的更新函数。此函数有一个unsigned short*参数,该参数包含图像的2D数据,每次调用更新时都会有所不同。在执行开始时,我希望将第一帧数据保存在一个单独的unsigned short*中,并且该数据在所有执行过程中都必须是活动的。

//安装程序在执行开始时运行一次

void Process::setup() 
{
...
_firstFrame = new unsigned short; //_firstFrame is an unsigned short* private variable from the class
return;
}
void Process::update(unsigned short* frame)
{   
//-- Performing an initial calculation before any further processing
if (!_condition)
{
//some processing that changes condition to true when criteria is met
if (condition)
memcpy(_firstFrame, frame, sizeof(640*480*sizeof(unsigned short)));
//each frame has 640*480 dimensions and each element is an unsigned short
return;
}
//further processing using frame
}

现在,_firstFrame应该始终保留在满足条件后产生的帧中的数据,但_firstFrame只包含零。有什么帮助吗?

您需要一个数组,但总是需要它,因此没有必要动态分配它。

你还需要初始化它,只初始化一次,所以你需要一些方法来跟踪它。目前你(试图)分配你的第一帧时,你不知道应该去它。

class Process {
bool got_first;
unsigned short first_frame[640*480];
public:
Process() : got_first(false) {}
void update(unsigned short *frame) {
if (!got_first) {
memcpy(first_frame, frame, sizeof(first_frame));
got_first = true;
}
}
};