动态创建指针数组

时间:2022-07-10 21:17:16

I am trying to create a array of pointers dynamically. From what I have read until now this can be done in the following way in C++

我正在尝试动态创建一个指针数组。从我到现在所读到的内容来看,这可以用以下c++实现

CPoint** data_temp;
data_temp = new CPoint*[an_integer]; 

I am later going to add pointers to this array which will be pointing to an object. Some what like this, using a for loop

我稍后将向这个指向一个对象的数组添加指针。有些像这样,使用for循环

 CPoint A;
 CPoint* data = &A; //I am using just a single value but data will also be an array
*data_temp[i] = data; // where data is pointer address

Now the question is if to free up memory I delete the pointer array data_temp would it also delete the original object i.e. A?

现在的问题是,如果要释放内存,我删除指针数组data_temp它是否也会删除原始对象,也就是A?

delete[] data_temp;

I am asking this because I need the object at a later stage.

我问这个是因为我在后面需要这个对象。

4 个解决方案

#1


2  

delete[] would be the thing to do if the array contained the objects themselves: those objects would be automatically destroyed. However, since this is an array of pointers, you'll need to do this to empty it out:

如果数组包含对象本身,那么delete[]就是要做的事情:这些对象将被自动销毁。然而,由于这是一个指针数组,您需要这样做来清空它:

for (int i=0; i < num_objects; i++)
{
    delete data_temp[i];
}

delete[] data_temp;

This would deallocate the memory pointed to by each of the objects, then delete the array itself.

这将释放每个对象指向的内存,然后删除数组本身。

EDIT: I just noticed that you allocated these objects on the stack. If you want to store them like this and manage the memory yourself, you should allocate them on the heap, like this:

编辑:我刚刚注意到你在堆栈上分配了这些对象。如果您希望像这样存储它们并自己管理内存,那么应该将它们分配到堆中,如下所示:

CPoint* A = new CPoint();
data_temp[i] = A;

...or, if you already have a CPoint on the stack and CPoint has a copy constructor...

…或者,如果您已经在堆栈上有一个CPoint,而CPoint有一个复制构造函数……

data_temp[i] = new CPoint(A);

#2


2  

No. It wouldn't. The delete applied to the array of pointer will delete an array of size (in your case) an_integer.

不。它不会。应用于指针数组的delete将删除大小(在您的例子中)的数组an_integer。

If you actually want to deallocate the memory these pointers point to, you woud have to go through a for loop

如果你想要释放这些指针指向的内存,你需要经过一个for循环

for(int i = 0; i < an_integer; i++){
    delete data_temp[i];
}
//Then deallocate the array
delete[] data_temp;

That means that in your case, you won't end up "deleting" the A object on the stack (something that would result in undefined behavior). You will only delete an amount of memory equal to the size of a pointer on your machine multiplied by an_integer.

这意味着在您的例子中,您不会最终“删除”堆栈上的A对象(这会导致未定义的行为)。您将只删除与计算机上的指针大小相等的内存,并将其乘以an_integer。

I'd say that in your case, an std::vector<A> would be justified and better.

我认为在你的例子中,std::vector是合理的,而且更好。

#3


1  

No, it wouldn't. You'd have to loop through the pointer array and delete the heap-allocated objects (ie. ones allocated with a call to new) individually.

不,它不会。您必须循环遍历指针数组并删除堆分配的对象(即。与调用new一起分配的)单独的。

#4


0  

You should use a typedef that's a perfect usage for them.

您应该使用类型定义,这是它们的完美用法。

typedef CPoint* PCPoint;

typedef CPoint * PCPoint;

After this treat PCPoint as array.

之后将PCPoint视为数组。

#1


2  

delete[] would be the thing to do if the array contained the objects themselves: those objects would be automatically destroyed. However, since this is an array of pointers, you'll need to do this to empty it out:

如果数组包含对象本身,那么delete[]就是要做的事情:这些对象将被自动销毁。然而,由于这是一个指针数组,您需要这样做来清空它:

for (int i=0; i < num_objects; i++)
{
    delete data_temp[i];
}

delete[] data_temp;

This would deallocate the memory pointed to by each of the objects, then delete the array itself.

这将释放每个对象指向的内存,然后删除数组本身。

EDIT: I just noticed that you allocated these objects on the stack. If you want to store them like this and manage the memory yourself, you should allocate them on the heap, like this:

编辑:我刚刚注意到你在堆栈上分配了这些对象。如果您希望像这样存储它们并自己管理内存,那么应该将它们分配到堆中,如下所示:

CPoint* A = new CPoint();
data_temp[i] = A;

...or, if you already have a CPoint on the stack and CPoint has a copy constructor...

…或者,如果您已经在堆栈上有一个CPoint,而CPoint有一个复制构造函数……

data_temp[i] = new CPoint(A);

#2


2  

No. It wouldn't. The delete applied to the array of pointer will delete an array of size (in your case) an_integer.

不。它不会。应用于指针数组的delete将删除大小(在您的例子中)的数组an_integer。

If you actually want to deallocate the memory these pointers point to, you woud have to go through a for loop

如果你想要释放这些指针指向的内存,你需要经过一个for循环

for(int i = 0; i < an_integer; i++){
    delete data_temp[i];
}
//Then deallocate the array
delete[] data_temp;

That means that in your case, you won't end up "deleting" the A object on the stack (something that would result in undefined behavior). You will only delete an amount of memory equal to the size of a pointer on your machine multiplied by an_integer.

这意味着在您的例子中,您不会最终“删除”堆栈上的A对象(这会导致未定义的行为)。您将只删除与计算机上的指针大小相等的内存,并将其乘以an_integer。

I'd say that in your case, an std::vector<A> would be justified and better.

我认为在你的例子中,std::vector是合理的,而且更好。

#3


1  

No, it wouldn't. You'd have to loop through the pointer array and delete the heap-allocated objects (ie. ones allocated with a call to new) individually.

不,它不会。您必须循环遍历指针数组并删除堆分配的对象(即。与调用new一起分配的)单独的。

#4


0  

You should use a typedef that's a perfect usage for them.

您应该使用类型定义,这是它们的完美用法。

typedef CPoint* PCPoint;

typedef CPoint * PCPoint;

After this treat PCPoint as array.

之后将PCPoint视为数组。