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). I have the following in one of my classes:
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.
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...)
delete() in C++, Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory. The safest way is to have the C++ program call a FORTRAN subroutine to delete the object. *** In your above guess at the C++ struct you are incorrect in assuming that a fortran array pointer is the same as a C++ item pointer.
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;
How do I clean up a dynamic array of pointers to objects?, private: OBJECT ** array; //declare pointer to pointer of OBJECT int array[c]; // deletes OBJECT if there's one at that element } delete array;� We can allocate and deallocate memory during the program’s execution using pointers. C++ provides two operators, new and delete, to create and destroy dynamically allocated variables. In C++, new and delete are reserved words. The operator new can both allocate single variables as well as arrays of variables.
If you want to delete objects in the heap:
for(int i = 0; i < totalInputs; ++i)
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.
Memory Management, C++ FAQ, Does delete p delete the pointer p , or the pointed-to-data *p ? Any time you allocate an array of objects via new (usually with the [ n ] in the new expression), � You delete each pointer individually, and then you delete the entire array. Make sure you've defined a proper destructor for the classes being stored in the array, otherwise you cannot be sure that the objects are cleaned up properly. Be sure that all your destructors are virtual so that they behave properly when used with inheritance.
Raw pointers (C++), A pointer can also be dereferenced to retrieve the value of the object that it Memory allocated using new must be freed by using delete (or delete). It's used in C-style programming to iterate over elements in arrays or� Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator; New operator is used for dynamic memory allocation which puts variables on heap memory. Which means Delete operator deallocates memory from heap.
delete Operator (C++), Using delete on a pointer to an object not allocated with new gives for the delete operator: one for single objects and the other for arrays of� The 3-D array arr can be considered as an array consisting of two elements where each element is a 2-D array. The name of the array arr is a pointer to the 0 th 2-D array. Thus the pointer expression *(*(*(arr + i ) + j ) + k) is equivalent to the subscript expression arr[i][j][k].
Pointers and Arrays in C++, You can create a new object for a pointer variable to point to, using a "new" operator that is similar to Another operator, named delete is provided for doing this. A member of a reference type, element of a managed array, or any other object that you can assign to a native pointer. Remarks. A pin_ptr represents a superset of the functionality of a native pointer. Therefore, anything that can be assigned to a native pointer can also be assigned to a pin_ptr. An interior pointer is permitted to perform the