我看过一些类似的问题,但没有解决方案适合我的情况。

我有一个具有可连续运行的更新功能的类。此函数有一个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;
    }
  }
};

09-04 03:05
查看更多