L
L
Lev K2017-09-20 19:58:51
C++ / C#
Lev K, 2017-09-20 19:58:51

How to correctly work with pointers in c++?

I am working with the OpenCV 2 version library.
With it, I get a pointer to the image:

IplImage *frame; //я же правильно понимаю, это указатель?

From here I can get some data, referring to it as an object, like:
int width = frame->width;
int height = frame->height;

What is the best way to work with this type of data? I create a rather large class for working with 1 frame, which I receive from the camera. Many different methods and temporary "images" - IplImage objects * (correct if I didn't put it right)
Now it's implemented something like this:
class opencv
{
private: IplImage *frame, *gray;

public: void getGray() {
this->gray = cvCreateImage(cvSize(this->frame->width, this->frame->height), this->frame->depth, 1);
cvConvertImage(this->frame, this->gray, CV_BGR2GRAY);
}
}

Those. I write to an already existing pointer, but you can return a pointer from a method, like:
public: IplImage * getGray() {
IplImage *gray = cvCreateImage(cvSize(this->frame->width, this->frame->height), this->frame->depth, 1);
cvConvertImage(this->frame, gray, CV_BGR2GRAY);
return gray;
}

But in this case, the pointer creation operation appears!
So here's the question:
How to work more correctly with this data type, like common sense says that the second option is "more logical" and clearer, especially when 10-15 methods for processing 1 frame appear. And each time there will be a creation of a new pointer. Is this a long operation? As is clear from the context, each ms is expensive, which is why I do not work with the Mat type, the same library, because the speed there is sooooo trembling.
or go according to the principle of the library itself, pass 2 pointers to the method: 1st input, 2nd output?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SolidMinus, 2017-09-20
@SolidMinus

To be honest, I didn't quite understand the question. so I will answer as I understand.
Creating a pointer is no different from creating another variable. This is just a variable that contains the value (4 bytes on x86, 8 bytes on x64) of the address of some memory cell. If this process is accompanied by memory allocation for something, then the process certainly becomes longer. But this does not go to any comparison with the speed in other languages, for example, interpreted ones. Therefore, there should not be a special zapar.
I don't quite understand what the problem is. In choosing how to return values ​​from a function?
And through this-> and through return the speed is the same. These are linear operators with O(1) complexity (like all assignments) and shouldn't be a concern either.
And, to be honest, I don’t understand at all why take on OpenCV, which is quite difficult for a beginner, if you don’t even understand what a pointer is?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question