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

问题描述 投票:-2回答:1

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

我想要一个C ++ 11解决方案,可扩展到指向类实例的指针数组,如果可能的话(我在这里使用了int来简化,我意识到使用类实例时可能还有其他问题)。

我知道如何创建一个固定大小的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"; 

}
c++ c++11 dynamic-memory-allocation unique-ptr dynamic-arrays
1个回答
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"; 

}
© www.soinside.com 2019 - 2024. All rights reserved.