How to make a copy of a pointer

I am trying to copy the data of pointers to another pointer, so if I change it, the value in the other will not change.

I need this because I am coding a loop where I have two pointers to a structure, a value and lastValue. In each iteration of the loop, I assign value content lastValue , and I populate value new content. The problem is that since both are pointers to a structure, when I change value , lastValue changes too, and that is not the behavior I want. The code will be something like this ( IplImages structures from OpenCV):

 IplImage *value; Iplimage *lastValue; while(1) { lastValue=value; value=cvQueryFrame( capture );//This fills the struct with new information } 

This will work if they have normal structures, but since they are pointers, both have the same meaning. Is there a way to get a copy of a pointer with the same value but with a different address?

+4
source share
3 answers

The cvQueryFrame function captures a frame from a camera or video file, unpacks and returns it. It returns a pointer to the internal OpenCV buffer that has the last captured frame. This should be the reason that you get the same value. If you need 2 frames, you must create a copy of the image.

 IplImage * previousFrame, *currentFrame ; CvCapture* video = cvCaptureFromAVI("video.avi"); currentFrame = cvQueryFrame( video ); // COPY IMAGE previousFrame = cvCloneImage( currentFrame ); while(currentFrame = cvQueryFrame( video )) { //... cvCopy( currentFrame , previousFrame); } //... 
+1
source
  value=cvQueryFrame( capture ); 

when I change the value, the lastValue value also changes

No, it is not. The value pointer is rewritten as you like. This line of code cannot affect lastValue .

but since they are pointers, both have the same meaning

No, it doesnโ€™t matter that they are pointers. Pointers are still objects in their own right.


However , cvQueryFrame returns a pointer to a buffer that you should not modify, or for free , as this is done for you:

Please note that the image captured by the device is highlighted / released by the capture function. There is no need to publish it explicitly.

Although the documentation is a bit unclear, it seems to me that the buffer is only valid until the next call to cvQueryFrame (which then reuses the allocated memory). Therefore, even if lastValue cannot and does not change, in any case, a new frame is still indicated.

To get around this, you can explicitly copy the object that lastValue points to:

 lastValue = cvCloneImage(value); 

Now you are probably taking responsibility for his release (but again, this is not entirely clear from my cursory glance at the documentation):

 cvReleaseImage(&lastValue); 
+2
source

You seem to be doing the right thing.

 last_value = value; //copy pointer 

but this is not exactly what you want, because it is not the pointer you want to copy, it is the data inside. why the old pointer is updated with the new pointer. I think you need a copy of the structure itself.

If the pointer points to some class that you are writing, add the copy constructor method to it, and then copy the value of the pointer like this:

 last_value = new MyClass(*value); //construct object, copy from what value points to //change value 
0
source

Source: https://habr.com/ru/post/1394858/


All Articles