C++ array of char using unique_ptr
Solution 1
Here is some code that demonstrates what i think you want:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <memory>
using namespace std;
using array_ptr_type = std::unique_ptr<char[]>;
using array_of_arrays_type = std::unique_ptr<array_ptr_type[]>;
auto main() -> int
{
auto x = array_ptr_type(new char[10]);
auto y = array_ptr_type(new char[10]);
for (int i = 0 ; i < 10 ; ++i)
{
x[i] = char('0' + i);
y[i] = char('0' + 9 - i);
}
auto pxy = array_of_arrays_type(new array_ptr_type[2]);
pxy[0] = move(x);
pxy[1] = move(y);
for (int i = 0 ; i < 2 ; ++i) {
copy(&pxy[i][0], &pxy[i][10], ostream_iterator<char>(cout, ", "));
cout << endl;
}
return 0;
}
expected output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Of course as you know, none of this is recommended - vector<vector<char>>
would be a lot cleaner and more maintainable.
Solution 2
You have to use specialization:
std::unique_ptr<char[]> chars(new char[1024]);
That is because std::unique_ptr
does not support custom deleter as std::shared_ptr
does (in this style of writing).
std::unique_ptr
uses std::default_delete
as deleter. Shortly, if you specify argument type as class T
it will use default delete
but if you write class T[]
(in this specialization) std::unique_ptr
will use delete[]
.
But it is better to use some container and not c-style arrays.
jelmew
Updated on July 01, 2020Comments
-
jelmew almost 4 years
First of all, I know this is not the best way to do this, I'm just looking how it should be done. I created a class called bord,which holds a member
std::unique_ptr<std::unique_ptr<char>[] > char_bord;
Which should be the correct syntax, then I try to initialize this in the constructor:
bord::bord():char_bord(new std::unique_ptr<char>[10]) { //char_bord=new std::unique_ptr<char>[10]; //This did not seem to work aswell. for(int i=0;i<10;i++) char_bord[i]=new std::unique_ptr<char>[](new char[10]); return; }
This results in the following heap of errors, which I did not manage to decipher.
jelmer@jelmer-N56JN:~/Git/Board/lib$ g++ -std=c++0x bord.c In file included from bord.c:1:0: bord.h:20:1: error: new types may not be defined in a return type class bord ^ bord.h:20:1: note: (perhaps a semicolon is missing after the definition of ‘bord’) bord.c:3:12: error: return type specification for constructor invalid bord::bord():char_bord(new std::unique_ptr<char>[10]) ^ bord.c: In constructor ‘bord::bord()’: bord.c:7:46: error: expected primary-expression before ‘]’ token char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:60: error: parenthesized initializer in array new [-fpermissive] char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:19: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<char>’ and ‘std::unique_ptr<char>*’) char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:19: note: candidates are: In file included from /usr/include/c++/4.9/memory:81:0, from bord.h:19, from bord.c:1: /usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = char; _Dp = std::default_delete<char>] operator=(unique_ptr&& __u) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::unique_ptr<char>&&’ /usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = char; _Dp = std::default_delete<char>] operator=(unique_ptr<_Up, _Ep>&& __u) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template argument deduction/substitution failed: bord.c:7:19: note: mismatched types ‘std::unique_ptr<_Tp, _Dp>’ and ‘std::unique_ptr<char>*’ char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ In file included from /usr/include/c++/4.9/memory:81:0, from bord.h:19, from bord.c:1: /usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = char; _Dp = std::default_delete<char>; std::nullptr_t = std::nullptr_t] operator=(nullptr_t) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::nullptr_t’
What am I doing wrong, assuming I am doing something wrong.
-
jelmew almost 9 yearsI kkow I should use a container. Could you elaborate on your answer? It it not totally clear to me. Is it just a smart pointer to a list of dumb pointers now?
-
Victor Polevoy almost 9 yearsWhat is use of
std::unique_ptr
and other smart pointers? Just to delete the pointer properly when it goes out of scope or it's reference count is zero. What yourstd::unique_ptr<std::unique_ptr<char>[] >
is for? Do you want deleteunique_ptr
when it ... I can't even describe that meaningless code. If you want to make a smart pointer to array of chars - use one smart pointer for an array. Otherwise I don't understand what are you trying to do. -
Puppy almost 9 yearsThis... is technically correct but does not explain all of the compiler errors the OP got in his question.
-
Victor Polevoy almost 9 years@Puppy I think if the author would rewrite his code to normal one (yes, my humble opinion) he will escape of all these errors because there are all about meaningless array's thing.
-
jelmew almost 9 yearsHi, Thanks, that was what I wanted to know, how it would be done. Indeed, back to the more sensible vector<vector<char> >. Thanks for answering my curiosity.