smart pointers and arrays
It will call delete[]
and hence the entire array will be reclaimed but I believe you need to indicate that you are using an array form of unique_ptr
by:
std::unique_ptr<int[]> my_array(new int[5]);
This is called as Partial Specialization of the unique_ptr
.
How to move to the next pointer in a smart pointer array
std::shared_ptr<char>
is a pointer to one char. When the shared pointer is destroyed it will call delete
on it's pointer, and not delete []
.
You could use a std::shared_ptr<char[]>
, then you could let that pointer hold an array of chars.
This is however the wrong tool for the job (unless you're working with legacy code or wrapping something that uses raw pointers to arrays). What you want to use is std::vector<char>
which is a dynamic array to chars with all the memory management abstracted away.
This is how you would use it in a loop, not really sure why you don't want to use the bracket notation as that is the tool for the job here.
#include <memory>
#include <iostream>
int main() {
std::unique_ptr<char[]> ptr(new char[10]);
for (int i=0; i<10; ++i)
ptr[i]='a'+i;
for (int i=0; i<10; ++i)
std::cout << ptr[i] << ' ';
}
The better option IMO is to instead use std::vector<char> ptr(10);
Can one declare an array of smart pointers to std::vector int on the stack?
//older method of declaring an array of std::vector<int> pointers with
//memory leak risks, using automatic (stack-based) allocation for the
//array of pointers, dynamic (heap-based) allocation for the vectors:
Automatic storage duration arrays don't leak. Their elements can, however. For example, when they are pointers that point to dynamically allocated elements. Smart pointers are there to replace raw pointers, so do just that - replace T*
with, for example, std::unique_ptr<T>
. Do not replace an array of automatic storage duration with a smart pointer - at most use std::array
. I believe you are looking for something like this:
std::unique_ptr<std::vector<int>> arr3[SIZE];
// or better - std::array<std::unique_ptr<std::vector<int>>, SIZE> arr3{};
for (int i = 0; i < SIZE; i++) {
arr3[i] = std::make_unique<std::vector<int>>(); // notice the syntax...
// ... and the fact that you first allocate the vector, *then* use the ->empty()
std::cout << &arr3[i] << std::endl;
if (arr3[i]->empty()) {
std::cout << "empty vector\n";
}
}
When should smart pointers be used to hold arrays?
Firstly, unique_ptr<T[]>
does not require a static size- it can be just unique_ptr<int[]>
.
Moreover, the added benefits are that std::array statically-allocates
its array
This is not strictly guaranteed to be a benefit. Consider if I have a 10-megabyte array- that's going to blow my stack.
Generally speaking, people choose this approach when they want the array size to be fixed on creation, but be able to mutate the members. Note that for std::vector
, you can only make both the elements and the vector
const
, or neither. You cannot make only the vector but not the elements const
.
How to declare an array over two dimensions with smart pointers?
May be this is what you are looking for but I do not think it is practical. You can use vector of vectors instead or some sort of other structures or libraries that can handle multi dimensional arrays.
int main()
{
unique_ptr<unique_ptr<int[]>[]> ptr(new unique_ptr<int[]>[5] {});
for (int i = 0; i < 5; i++)
{
unique_ptr<int[]> temp_ptr(new int[5]);
for (int j = 0; j < 5; j++)
temp_ptr[j] = i*5 + j;
ptr[i] = move(temp_ptr);
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
cout << ptr[i][j] << " ";
cout << "\n";
}
}
Smart pointer 2D array as a parameter
std::shared_ptr<std::shared_ptr<int[]>[]>
is the type of the thing you want to pass to the function, not int[4][5]
. Also, since a std::shared_ptr<>
doesn't know how many consecutive thingies there are at the memory location it points to, you also have to pass the dimensions to the function.
Create an array of smart pointers to a class with no default constructor
You can't do that with make_unique
. But you can use this:
unique_ptr<A[]> ptr(new A[3]{{"A"}, {"B"}, {"C"}});
Before C++11 - it was very hard (that can be done with placement new etc).
Related Topics
Std::List<>::Sort()' - Why the Sudden Switch to Top-Down Strategy
Why Is Protected Constructor Raising an Error This This Code
Dereferencing an Invalid Pointer, Then Taking the Address of the Result
Why Do We Need to Use Boost::Asio::Io_Service::Work
Which Standard Wording Tells Us That Ref-To-Const Temporary Lifetime Extension Only "Works Once"
Why Is This Code Trying to Call the Copy Constructor
Access Array Beyond the Limit in C and C++
Issue When Scheduling Tasks Using Clock() Function
Getdibits and Loop Through Pixels Using X, Y
Using Assignment as a Condition Expression
Why How to Not Brace Initialize a Struct Derived from Another Struct
Is It Reasonable to Use Std::Basic_String<T> as a Contiguous Buffer When Targeting C++03
Why Should Default Parameters Be Added Last in C++ Functions
Measuring Memory Bandwidth from the Dot Product of Two Arrays
Std::Vector and Contiguous Memory of Multidimensional Arrays
C++: Converting Hexadecimal to Decimal
Parameter Pack Must Be at the End of the Parameter List... When and Why