1

I used to allocate memory in my C++ project with new

char* buffer = new char [size];
...
delete[] buffer;

and I'd really like to move forward and use unique_ptr, like this

unique_ptr<char[]>buffer(new char[size]);

but then I use istream& get (char* s, streamsize n); which takes char* as a first argument, so what should I do? I've tried to cast types, but failed. I also know I can use vector<char> instead of pointers, but I don't really like to use it. Thank you!

Alexandr
  • 85
  • 2
  • 13
  • http://stackoverflow.com/questions/16711697/is-there-any-use-for-unique-ptr-with-array – AnatolyS Jun 17 '16 at 07:42
  • 1
    Possible duplicate of [Use unique\_ptr for ownership and raw pointer otherwise?](http://stackoverflow.com/questions/12555441/use-unique-ptr-for-ownership-and-raw-pointer-otherwise) – The Forest And The Trees Jun 17 '16 at 07:44
  • 5
    When in doubt about how to use a class, it's a good idea to consult some documentation about it, such as [cppreference](http://en.cppreference.com/w/cpp/memory/unique_ptr/get). – Angew is no longer proud of SO Jun 17 '16 at 07:45
  • you shouldn't use `new`, use [`make_unique`](http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique). – Incomputable Jun 17 '16 at 07:49
  • 1
    What is the point against `std::vector` or even `std::string`? Vector is a block of continuous memory. It is exactly what you should be using. Except for statically allocated arrays, there you could go with `std::array`. – Jens Jun 17 '16 at 08:00

3 Answers3

10

The class std::unique_ptr has a method called get() to access the underlying pointer: use that.

unique_ptr<char[]> buffer(new char[size]);
...
myIstream.get(buffer.get(), n);
Smeeheey
  • 9,162
  • 16
  • 37
  • But when I try to pass the pointer to library function, fetching from get(), the application crashes. – Pabitra Dash Feb 04 '19 at 01:26
  • That could be for all sorts of reasons depending on the details, not necessarily related to the original question. Perhaps you could post your own new question? – Smeeheey Feb 04 '19 at 13:23
1

There is a std::unique_ptr<T[]> specialization for arrays. How to use it in your case in shown by Smeehey in his answer.

But, as of Scott Meyer's "Effective Modern C++" Item 18 (Emphasis mine):

The existence of std::unique_ptr for arrays should be of only intellectual interest to you, because std::array, std::vector, and std::string are virtually always better data structure choices than raw arrays.

So, use the classes that are made for this purpose instead of hacking your way there with std::unique_ptr.

LukeG
  • 605
  • 10
  • 19
0

One of the way is to use vector rather than unique_ptr, but using unique_ptr has it's own benefits.
I also faced the same issue earlier, in which I had a buffer allocated using unique_ptr and passed it as a argument to read function. The only way to get rid of this is using the get() method which converts the unique_ptr to char*. Also make sure you have #include <memory> to avoid other errors which I faced.

Thanks!