0

Problem

I have an old code base that is processing images. The C++ style is old (before C++11), there are no smart pointers or anything like that. Only a lot of dynamic allocation and raw pointers.

There is always only one actual image that is processed. These images are kept and passed as c-style arrays:

class A {
    A(double* image);
}

class B {
    B(){ image = new double[10]; };
    double* image;
} 

class C {
    C();
    foo(double* image);
}

I need to add a new class that manages and reads in images. And I would like to set up a class like this, because I feel pretty uncomfortable with all this dynamic allocation and would like to use more modern C++.

class NewImage {
     NewImage();
     double* getImage() { return &image.get()->at(0); };
     readImage();
     ...
private:
     std::unique_ptr<vector<double>> image;
}
  1. Is it recommended to have such a getImage() function for handing over the image to the old part? Or should I use something like std::unique_ptr<double> image?
    • If I would use std::unique_ptr<double> image, would I need a custom deleter because the unique_ptr manages an array, and not a pointer to double?
  2. Or shouldn't I use modern C++ at all and stick to the old style? With double* image and dynamic allocation?
6
  • 6
    One seldom, if ever, need a pointer to a vector. Sometimes one could need a vector of pointers, but this doesn't seem to be such a case. All you need is a plain vector. Commented Sep 3, 2019 at 13:25
  • 1
    You don't need pointers in the C++ implementation of this code. To save overhead have the functions take the vector by reference. If you need to give the vector to another object, then you can use move semantics to move it so you don't have the cost of copy. Commented Sep 3, 2019 at 13:26
  • To expand on @Someprogrammerdude's comment : it's incredibly hard to justify a std::unique_ptr<std::vector> because there are essentially no added benefits to using it over a std::vector> (except maybe that it's nullable). But the use cases for a std::shared_ptr<std::vector> are a bit more common. Commented Sep 3, 2019 at 14:07
  • @NathanOliver What do you mean by "C++ implementation of this code"? A, B and C can't be changed, this would mean ra re-write of the whole application. I need an interface between NewImage and the old functionality, that only accepts double*. Commented Sep 3, 2019 at 14:08
  • @Someprogrammerdude I wanted to use unique_ptr<vector> to demonstrate that NewImage is the owner of this image data and manages it for the whole application. Others mustn't be possible to set this to another image. I wanted to prohibit that someone from somewhere adds a setter and overwrites the actual image. Commented Sep 3, 2019 at 14:14

1 Answer 1

3

Is it recommended to have such a getImage() function for handing over the image to the old part?

If you have use for such function, then sure. A more modern alternative could be to return a span that encapsulates pointer to the beginning as well as the length of the array.

Or shouldn't I use modern C++ at all and stick to the old style? With double* image and dynamic allocation?

Dynamic allocation is still used with the newer design. It's just hidden inside std::vector.

Note that std::vector has been in the C++ standard since the beginning, and managing dynamic array without a container is very old style pre-dating standard C++ (that's over two decades old now) and the STL before that. I don't know good arguments for staying with the old style.


P.S. There's probably no advantage for using the additional indirection of std::unique_ptr. It is probably more optimal to store the vector as a member directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.