Reputation: 4683
So say I have
foo* fooObject1 = new foo[10];
// here would go code for initializing fooObject1[0] to fooObject1[9] with a foo object for each.
foo* fooObject2 = new foo[30];
for(int = 0; i < 10; i++)
fooObject2[i] = fooObject1[i];
if(fooObject1 != NULL)
delete[] fooObject1; //this "destroys" the array, BUT ALSO calls the destructors of all the foo objects inside fooObject1, causing fooObject2 elements to be anything but what it was before.
fooObject1 = fooObject2;
where foo
is a specific object created by me with its respective methods.
My problem here, however, is that I want to destroy the array that fooObject1 points to, but not the elements of the array, is there a way for that to be done in C++? I know another way to go around the problem is to overload the = (equals) operator, but in the program I need it for, writing that would include overloading many other = operators for many other classes, which makes it a tedious and long process. I was hoping that in some way I could keep the objects in fooObject2, but get rid of the array pointed by fooObject1. So can this be done? How? If not how, then are there are websites you could redirect me to read about it?
Upvotes: 3
Views: 1614
Reputation: 42964
You may want to use modern C++ techniques and containers like std::vector
(instead of raw exception-unsafe leak-prone new[]
and delete[]
and raw pointers).
If you want arrays of pointers to foo
objects, and you want these foo
objects to be shared between these arrays, you may consider a vector<shared_ptr<foo>>
.
shared_ptr
is a smart pointer template class, that uses reference counting, and destroys the pointed object only when the last reference to the object is released.
You can use make_shared
allocator to instantiate new instances of foo
, and put them in the (dynamic) arrays using vector::push_back()
.
e.g.
#include <vector> // std::vector
#include <memory> // std::shared_ptr, std::make_shared
using namespace std;
// Smart pointer to foo
typedef shared_ptr<foo> foo_ptr;
// Dynamic arrays of smart pointers
typedef vector<foo_ptr> foo_array;
// Define an array of smart pointers to foo
foo_array v1;
// Populate the array
v1.push_back( make_shared<foo>( /* some constructor init params */ ) );
v1.push_back( make_shared<foo>( /* ... */ ) );
// Create another array of smart pointers to foo's
// and reference the foo's created in v1
foo_array v2 = v1;
// Empty the array of smart pointers 'v1'.
// However, the 'foo' objects previously created are still "alive",
// because of the references stored in 'v2'.
v1.clear();
Note that using modern C++ techniques: there is no explicit delete
; resources are managed automatically thanks to the STL containers, smart pointers and the power of C++ destructors; the code is leak-free and exception-safe, easier to read and maintain.
Upvotes: 1
Reputation: 66224
These are independent. Your fooObject2 array has been allocated just as fooObject1 was, and assignment operators fired for each all along the way. you effectively have a copy of each object from fooObject1 in fooObject2.
you cannot abandon fooObject1. You need to delete[] it or you'll leak memory. fooObject2, as written, is independent unless you have internal pointers in the fooObject class and a shallow copy was performed because you didn't follow the Rule of Three.
The last line of this sets fooObject1 (the pointer) to point to the same objects as fooObject2 after you cleaned up fooObject1 and deleted its prior memory allocation, just FYI, but do NOT delete them BOTH after that reference is made; only delete ONE.
Upvotes: 2
Reputation: 4217
In C++, arrays are not objects. What you have is two pointers which, because you called new[]
, happen to point to the beginning of n-number of contiguously allocated objects. You can not delete the array and not delete the objects. The array "is" the contiguous memory where the objects are located.
You can, however, clear the pointer, but doing so means you no longer know where the objects are located. This is what is called a memory leak. That memory was allocated for those objects and you can no longer deallocate it because you do not know where it is.
You did copy the values of 10 objects from fooObject1
to fooObject2
. Deleting fooObject1
will not change the objects in fooObject2
because they are different objects - fooObject2
points to a different array in a different memory location with a different set of objects. You preserved the values that used to be fooObject1
by assigning the values of those objects to the values of the objects in fooObject2
.
Upvotes: 1
Reputation: 241771
The array pointed to be fooObject1 contains the objects. You're effectively asking a carpenter to destroy a bookcase but leave the shelves with the books on them. (Nice trick if you can do it.)
In your for loop, you then copy the objects from fooObject1 to fooObject2. So fooObject2 has different objects in it.
If you wanted the objects to be independent of their containers, you should use pointers.
Upvotes: 2