正在将指针指向的数据复制到另一个指针中
Copying data pointed at by pointer into another pointer
我已经研究了一些类似的问题,但没有解决方案适用于我的案例。
我有一个类,它有一个连续运行的更新函数。此函数有一个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;
}
}
};
相关文章:
- C++指针复制重载
- 将值从指针复制到常量对象参数
- 在 C++ 中通过字符串指针复制字符串变量中的字符串
- 使用指针复制构造函数用法
- 将对象的指针复制到此对象的新指针中
- 如何将指向 int(数组)的指针复制到 int 数组?
- 从指针复制对象
- C 使用指针复制char*
- 使用随机指针C 复制列表
- 如何在C++中从void指针复制数据
- 在 C++ 中将一个指针复制到另一个指针
- 正在将指针复制到assign运算符中的另一个列表元素
- 从压缩的DIB无效指针复制
- 无法从字符指针复制到短
- C++使用指针复制构造函数
- 将对象指针复制到动态对象数组中
- 在 CUDA 中使用指针复制类
- 从指针复制派生对象
- 如何使用智能指针复制C++更改指针
- 将内容从一个指针复制到另一个指针