0

I'm still new to manual destruction in C++ (came from languages with garbage collection). 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.

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.

Phillip McMullen
  • 303
  • 3
  • 13
  • 3
    In modern C++ you should prefer `std::vector` and smart pointers over manually managing memory – UnholySheep Mar 06 '19 at 23:16
  • 1
    The best advice is a slight extension to @UnholySheep 's comment: Don't `new` stuff unless you really really have to. Recommended reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Mar 06 '19 at 23:25
  • Sorry, I forgot to include - this is on a microcontroller which, to my knowledge, doesn't support smart pointers. I'll update the tags/question. Thanks for your responses, however – Phillip McMullen Mar 06 '19 at 23:26
  • On a microcontroller you are even less likely to want to be allocating and de-allocating objects frequently (if at all). It runs the risk of fragmenting the memory. – user4581301 Mar 06 '19 at 23:29
  • Does your Arduino compiler support `C++11`? If it does, writing a `std::unique_ptr` type smart pointer isn't too hard. – Galik Mar 06 '19 at 23:45
  • It looks like it does support `C++11` but i don't have `std::unique_ptr`, do you mean to create my own? If so, can you point me in the right direction for this? – Phillip McMullen Mar 07 '19 at 16:42

3 Answers3

5

Its best not to use the raw pointers at all and go for stl containers. 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;

cplusplusrat
  • 1,327
  • 9
  • 19
1

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...)
Andy Thomas
  • 78,842
  • 10
  • 93
  • 142
-1

If you want to delete objects in the heap:

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.

  • 1
    This has no context within the code, please provide some explanation on what you are trying to achieve. – Sailanarmo Mar 06 '19 at 23:33
  • I thought he was asking how to delete old objects? – Alzer Casiño Mar 07 '19 at 00:23
  • That is not what I was getting at. You have code, it does a thing, what does it do? Why is it correct? What is the context or subject? Basically, you need to explain yourself. Look at Andy's and cplusplusrat's answers. They have code, but they give explanations on why their answers are correct, and explain what they are doing. – Sailanarmo Mar 07 '19 at 00:30