简体   繁体   English

如何从指向指针数组的指针中删除对象?

[英]How can I delete an object from a pointer to a pointer array?

I'm still new to manual destruction in C++ (came from languages with garbage collection). 我仍然不熟悉C ++中的手动销毁(来自具有垃圾回收功能的语言)。 I have the following in one of my classes: 我的一门课有以下内容:

Input** Inputs;

Which gets initialized as follows: 初始化如下:

this->Inputs = new Input* [totalInputs];

And can get reassigned to later on in my code depending on user input, similar to this: 并可以根据用户输入稍后在我的代码中重新分配,类似于:

this->Inputs[inputNumber] = new DigitalInput(params...)

The problem with this is that it's open to memory leaks when reassigning the object at that location due to releasing the old object. 问题在于,由于释放旧对象,在该位置重新分配对象时,它很容易发生内存泄漏。

What is the best way to delete the old object once it's been reassigned? 重新分配旧对象后,删除旧对象的最佳方法是什么?

Edit: I forgot to include that this is on an AVR microcontroller which is running on the Arduino codebase. 编辑:我忘了包括在Arduino代码库上运行的AVR微控制器上。

Edit 2: The reason that I'm doing this this way is because the user is allowed to send commands to the unit which will change the input type (ie: send a command and this->Inputs[inputNumber] = new AnalogInput(params...) . Also the reason it's a pointer to an array of pointers is because the constructor for this object will generate that array based on the totalInputs argument passed in. This is in a shared library which is used on a few different units. 编辑2:之所以这样做,是因为允许用户将命令发送到将更改输入类型的单元(即:发送命令,并且this->Inputs[inputNumber] = new AnalogInput(params...)也是指向指针数组的指针的原因是因为此对象的构造函数将基于传入的totalInputs参数生成该数组。这在一个共享库中使用,该共享库可在几个不同的单元上使用。

Its best not to use the raw pointers at all and go for stl containers. 最好不要使用原始指针,而是使用stl容器。 One possible way could be as below. 一种可能的方式如下。

using InputPtr = std::unique_ptr<Input>;
std::vector<InputPtr> Inputs;
Inputs.emplace_back(std::make_unique<DigitalInput>());

No need to worry about memory leaks any more. 无需担心内存泄漏。 The other option you have is to use std::shared_ptr depending upon how you intend to use your InputList; 您还有另一个选择是使用std::shared_ptr这取决于您打算如何使用InputList。

If you're reassigning a member of the array to point to a new object, you can first deallocate the old object, if any. 如果要重新分配数组的成员以指向新对象,则可以首先取消分配旧对象(如果有)。

Input* oldInput = this->Inputs[inputNumber];
delete oldInput;

this->Inputs[inputNumber] = new DigitalInput(params...)

If you want to delete objects in the heap: 如果要删除堆中的对象:

for(int i = 0; i < totalInputs; ++i) delete Inputs[i]; delete[] Inputs for(int i = 0; i < totalInputs; ++i) delete Inputs[i]; delete[] Inputs ; for(int i = 0; i < totalInputs; ++i) delete Inputs[i]; delete[] Inputs

Edit: If you're using a microcontroller it would be best to allocate at the stack instead. 编辑:如果您使用的是微控制器,则最好在堆栈上分配。

Define a maximum size on your array. 在阵列上定义最大大小。 Like: 喜欢:

const int MAX = 5;
Inputs inputs[MAX][MAX];

then just asign objects to it. 然后将对象分配给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM