我已经看过一些类似的问题,但没有解决方案正在为我的案例工作。将指针指向的数据复制到另一个指针
我有一个类有一个连续运行的更新函数。该函数有一个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只包含零。 有什么帮助吗?
您只为一个未签名的short分配空间。想必你想要一个数组。 – ooga
你想从*'_firstFrame'复制*到*或*吗?请记住'memcpy'的目标参数是第一个。 –
是的,我会编辑这个问题,因为这个代码被复制粘贴后,我切换他们检查,如果我是错误的传递参数,显然产生了分段错误。 –