Answer the question
In order to leave comments, you need to log in
How to delete an array of objects?
I have an array of objects.
I throw it into a function. It turns out void foo(MyClass *&arr){//...}
This function creates a new array and writes data from the old one there, but not all. Until a certain point.
But now I need to remove the old array from my pointer, and write the address of the new one into it!
How to do it? Help me please.
I tried to do it just
delete[] arr;
arr = new_arr;
Answer the question
In order to leave comments, you need to log in
If you have an array of pointers to objects.
Objects can be deleted in the
delete arr[i] loop. You also need to pass the size of the array to the function.
But it's better to use smart pointers and standard containers.
For example https://en.cppreference.com/w/cpp/memory/unique_ptr
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question