在c ++ 11中创建一个unique_ptr动态数组的正确方法是什么?

时间:2019-04-07 17:42:58

标签: c++ c++11 dynamic-memory-allocation unique-ptr dynamic-arrays

我想确定一种规范的方法来分配等效于指向int的指针的数组(例如:int ** int_array),但是要使用unique_ptr。

我想要一个C ++ 11解决方案,如果可能的话,该解决方案可以扩展为一个指向类实例的指针的指针数组(我在这里使用ints来简化,我意识到当使用类实例)。

我了解如何创建一个预先确定大小的unique_ptr固定大小的数组。目标是在未知数组大小的地方做同样的事情。

我已经研究了许多相关的解决方案,包括下面的解决方案,但是它们似乎仅处理unique_ptr数组的固定分配(也就是说,unique_ptr数组的大小已经预先知道):

Proper way to create unique_ptr that holds an allocated array

我已经实现了一个简单的程序,试图演示和比较3种方法:传统的动态指针创建,固定的unique_ptr数组和目标:动态的unique_ptr数组。

#include <iostream> // include iostream
#include <memory> // include memory

using namespace std;

int main() {

  cout << "Testing dynamic arrays of pointers\n";

  int **num_array; // typical dynamic array of pointers to int
  int count;       // count of ints the user wants to generate

  unique_ptr<int[]> f_num_array(new int[200]()); 
       // above: fixed array of unique pointers to int - not what I want

  unique_ptr<int[]> u_num_array; 
       // above:  GOAL: dynamic array of unique pointers to int

  int sum, u_sum, f_sum; 
       // above: test sum of each type of array (should match user count)

  cout << "How many pointers would you like? ";

  cin >> count; // get user input

  num_array = new int*[count]; // allocate array of pointers on heap

  //u_num_array = new int[count](); // GOAL - would like to do this
       // above: ERROR: no overload for =; cannot allocate this way

  for(int i=0; i<count; i++) { // allocate ints and store pointer
    num_array[i] = new int(1); // pointer to an int on the heap 
    f_num_array[i] = 1; // assign 1 to the pre-allocated unique pointer array

    unique_ptr<int> u_tmp(new int(1)); // temporary unique_ptr to int
    // u_num_array[i] = u_tmp; // GOAL - would like to do this...
        // ERROR: cannot assign unique_ptr this way
  }

  sum = 0; f_sum = 0; u_sum = 0; // init our sums to verify

  for(int i=0; i<count; i++){
    sum += *(num_array[i]); // summing our traditional array of pointers
    f_sum += f_num_array[i]; // summing our fixed unique array of pointers
  }

  cout << "Sum = " << sum << "\n";
  cout << "Sum (fixed unique_ptr array) = " << f_sum << "\n";
  cout << "Sum (dynamic unique_ptr array) = " << u_sum << "\n";

  delete[] num_array; // delete the dynamic array
  f_num_array.release();  // delete the dynamic array

  cout << "\nDone!\n"; 

}

1 个答案:

答案 0 :(得分:3)

#include <iostream>
#include <memory>
#include <vector>

int main() {

  std::cout << "Testing dynamic arrays of pointers\n";

  //int **num_array; - Never use except you are forced by some external interface.
  int count = 0; // Always initialize variables

  std::vector<std::unique_ptr<int>> num_array; 

  std::cout << "How many pointers would you like? ";

  std::cin >> count; // get user input

  num_array.resize(count);
  for (auto& p : num_array) // You can do it with some algorithm, but I prefer this way
    p = std::make_unique<int>(1);

  int sum = 0; // Don't declare variables before you use them.
  for (auto& p : num_array)
    sum += *p;

  std::cout << "Sum = " << sum << "\n";

  num_array.clear();

  std::cout << "\nDone!\n"; 

}