3

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?

3
  • Yes, cuz the way I understood in my class is that once fooObject1 = new foo[10] is executed, there is an array of length 10 that occupies space in the memory, despite of not having any elements yet. So I was planning to get rid of that array, but not the elements (which later on are added).
    – Ren
    Sep 29, 2012 at 4:46
  • Not sure who you're responding to, but that's not quite accurate. There are 10 foo objects created when you created the array. They exist. If you put a breakpoint in foos default constructor, you will hit that breakpoint 10 times during the first new[] call. Sep 29, 2012 at 4:51
  • Continuing where @SionSheevok left off, you'll hit it 30 more times when creating the array pointed to by fooObject2. these are independent allocations. The only thing i see here that is suspect is the assignment operator. If there are any dynamic allocations in your class and you didn't override operator=() you will shallow-copy those pointers, and when destructor time comes you're in a world of hurt once fooObject1 is gone and fooObject2 remains (smart pointers not withstanding).
    – WhozCraig
    Sep 29, 2012 at 4:56

4 Answers 4

2

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.

2
  • Yes, apparently I haven't followed that rule of three, (i just realized that) it indeed has made a shallow copy, to make a deep copy of it would mean overloading the = operators of foo and many other objects inside foo, which will take me very-very long, but I suppose there is no other way to do it by looking at the answers.
    – Ren
    Sep 29, 2012 at 4:56
  • it may not be as bad as you think. Commit the RO3 to memory, and think about member variables that perhaps already practice it. You may find it isn't difficult to get your class to follow it as well if you use members that already do.
    – WhozCraig
    Sep 29, 2012 at 5:02
2

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.

2
  • Now that I think of what you say, it makes sense, because the objects in fooObject2 still exist, but their internal variables and objects and pointers are all messed up with characters that shouldn't be there at all. Thanks for the tip. So how do I make an pointer to an array of pointers? I've never done that. Sorry, I'm a noob I guess.
    – Ren
    Sep 29, 2012 at 4:52
  • foo **fooarr = new foo*[10];, but you now have to initialize each element.
    – nneonneo
    Sep 29, 2012 at 4:53
1

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.

1

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.

1
  • I would, but this assignment I have to do has me strictly prohibited from using other libraries other than <ostream>
    – Ren
    Sep 29, 2012 at 19:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.