C++中动态分配的结构指针数组

Dynamically Allocated Array of Struct Pointers in C++

本文关键字:指针 数组 结构 动态分配 C++      更新时间:2023-10-16

好吧,我对c++很陌生(我认为我们正在学习的是c和c++的混合)
我已经找到了很多答案来回答我的问题,遗憾的是,所有这些答案都在C中使用malloc。

struct A {
    int randomStuff = 0;    
};
struct B {
    int numOfA= 5;  // In reality this number is variable.
    A** arrayOfA;   
};

结构是给我们的。现在我需要分配并用指向A指针的指针填充这个数组<-如果我错了,请在这里纠正我。指针对我来说仍然很复杂。

A * a1 = new A;
A * a2 = new A;
B * b = new B;
// Allocate space for the array...
b->arrayOfA = new A*[numOfA];
// Next I want to initialize the pointers to NULL
for(int i; i < b->numOfA; i++){
    b->arrayOfA[i] = NULL;
}
// In another function I would the assign a value to it
b->arrayOfA[0] = a1;
b->arrayOfA[1] = a2;

我认为b->arrayOfA需要指向一个结构的数组。。。有点像这个

b->arrayOfA = new A*;
A * arr[numOfA];
b->arrayOfA = arr;

我的大脑在流血
如何正确分配它并为其分配现有值(结构)

*编辑
代码似乎按预期工作,而我的显示给我带来了问题。基本上,我需要一个数组"arrayOfA[]",在其中我可以放置指向A结构的指针。有效地做到这一点:

cout << arrayOfA[0]->randomStuff // 0 would be displayed

成为0。

您可以分配一个指针数组,并为每个指针分配一个对象数组

int x = 5, y = 6;
b->arrayOfA = new A*[x]; //array of pointers
for(int i=0;i<x;i++){
    b->arrayOfA[i] = new  A[y]; //matrix (array of arrays)
}
for(int i=0;i<x;i++){
    delete[] b->arrayOfA[i]; //don't forget to free memory
}
delete[] b->arrayOfA;

您应该能够只使用向量:

#include <vector>
int main()
{
    vector<A> vector_of_a;
    vector_of_a.push_back(a1); //store a1 in the vector
    vector_of_a.push_back(a2); //store a2 in the vector
    //...
    std::cout << "Number of A's: " << vector_of_a.size() << std::endl;
    std::cout << vector_of_a[0].randomStuff << std::endl;  //prints 0 as specified but with '.' not '->'  Objects are still on the heap, and not the stack.
}

向量中的A存储在堆中,但您不需要自己管理内存(不需要malloc/freenew/delete。当矢量超出范围时,A对象将被正确处理
您还可以获得

您也可以插入指向对象的指针,但这会降低vector的有用性,因为您必须为对象进行自己的内存管理:

#include <vector>
int main()
{
    A* a1 = new A();
    A* a2 = new A();
    vector<A> vector_of_a;
    vector_of_a.push_back(a1); //store pointer to a1 in the vector
    vector_of_a.push_back(a2); //store pointer to a2 in the vector
    //...
    std::cout << "Number of A's: " << vector_of_a.size() << std::endl;
    std::cout << vector_of_a[0]->randomStuff << std::endl;  //prints 0 as specified
    //...
    for (auto i : vector_of_a)
    {
        delete (i);
    }
    vector_of_a.clear();
}

如果你真的不想使用向量,那么我建议你把struct B变成一个成熟的类
它提供了封装的好处,管理数据的函数存储在类中,类进行内存管理,而不是将其留给用户管理和清理代码:

class B
{
public:
    array_of_A(unsigned int size);
    ~array_of_A();
    bool init_array();
    unsigned int get_size();
    A** get_array();
private:
    unsigned int num_of_A;
    A** array_of_A;
}
B::array_of_A(unsigned int size)
{
  num_of_a = size;
  array_of_A = new A*[size];          //create array
  memset (array_of_A, nullptr, size); //initialise contents to nullptr
}
B::~B()
{
    for(unsigned int i = 0; i < num_of_a; i++)
    {
        delete array_of_a[i]; //delete each A
    }
    delete[](array_of_a);     //delete the array of pointers.
}
unsigned int B::get_size()
{    return num_of_A;    }
A** B::get_array()
{    return array_of_A;    }
int main()
{
    B b((5));     //most vexing parse...
    b.get_array()[0] = new A();
    b.get_array()[1] = new A();
    b.get_array()[2] = new A();
    std::cout << b.get_array()[0]->randomStuff << std::endl //prints 0 as requested
}   //b goes out of scope, destructor called, all memory cleaned up

aa通过内部化内存管理和支持任意长度的数组,我们刚刚开始实现一个(非常)更简单的vector版本。但有利于练习/学习。