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.

6
  • 3
    In modern C++ you should prefer std::vector and smart pointers over manually managing memory Commented Mar 6, 2019 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'? Commented Mar 6, 2019 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 Commented Mar 6, 2019 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. Commented Mar 6, 2019 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. Commented Mar 6, 2019 at 23:45

3 Answers 3

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;

Sign up to request clarification or add additional context in comments.

2 Comments

This I can get behind.
That said, it does beg the question, Does the asker need dynamic allocation at all?
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...)

Comments

-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.

3 Comments

This has no context within the code, please provide some explanation on what you are trying to achieve.
I thought he was asking how to delete old objects?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.