Pedro Batista
Pedro Batista

Reputation: 1108

Copying data pointed at by pointer into another pointer

I've looked at some similar questions but no solutions is working for my case.

I've got a class that has an update function that runs continously. This function has an unsigned short* argument that contains the 2D data of an image, which is different every time update is called. At the beggining of execution I want to keep the first frame data in a separate unsigned short*, and this data must be alive through all the execution.

//setup runs once at the beginning of execution

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
}

Now, _firstFrame is supposed to always keep data from that frame that originated after condition was met, but _firstFrame only contains zeros. Any help?

Upvotes: 0

Views: 124

Answers (1)

Useless
Useless

Reputation: 67852

You need an array, but you always need it so it isn't necessary to allocate it dynamically.

You also need to initialise it, exactly once, so you need some way to track that. Currently you (try to) allocate your first frame when you don't know what should go in it.

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;
    }
  }
};

Upvotes: 2

Related Questions