Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique_ptr to char* conversion

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!

like image 961
Alexandr Avatar asked Dec 14 '22 05:12

Alexandr


1 Answers

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);
like image 89
Smeeheey Avatar answered Jan 01 '23 00:01

Smeeheey