Do i need to memory delete this vector of pointers?

Hello,

i have the following code

1
2
3
4
5
6
7
8
9
10
11
12
std::vector<MyClass*> myClassList;
anotherObject->GetMyClassList(myClassList);

...
// no longer need the myClassList but dont want to delete any MyClass obj
myClassList.clear() // needed? harmful for other lists with pointers to myclass objects?)


signature:

void	GetMyClassList(std::vector<MyClass*>& myClassList);


When i call GetMyClassList to retrieve a list of pointers of MyClass, do i have to somehow delete the vector to free memory? I dont want to delete the Objects in the vector because "anotherObject" needs them, the GetMyClassList only adds a few from a global list to the myClassList. I just want to make sure that the vector itself which only contains a references to pointers doesnt need to be deleted in anyway. So calling myClassList.clear() would not call the deconstructor ob the MyClass obj and only delete the pointer references - but since there is another list also with pointers to these objects - would these be invalidated? dont i need to do anything special here because once the myClassList runs out of scope it will take care of everything?

Thanks
If you didn't create an object with new, you can't delete it.

but since there is another list also with pointers to these objects - would these be invalidated?

No, why would they be?
closed account (zb0S216C)
Majin wrote:
do i have to somehow delete the vector to free memory?

No. The std::vector will automatically de-allocate the memory it uses. However, don't do this:

 
std::vector<int *> Ints(2, new int(0));

It defeats the purpose of std::vector. Why? Internally, a std::vector allocates a large block of memory. It uses the memory to store any objects that get pushed into the vector. By allocating a new object with each push, you're effectively doubling the memory used by the vector (if the vector is full). std::vector will not automatically de-allocate the memory, so your program will leak.

Majin wrote:
contains a references to pointers

There can be no arrays of references. Though, by looking at your code, you meant a pointer to something.

You seem state some theories about how your program should work. Be careful, not all theories are correct; hence theory.

Wazzak
Thank you for your replies - so to sum it up :

is myClassList.clear() not needed at all?
does a vector not consume any memory footprint even if it only contains pointers to objects?
Last edited on
closed account (zb0S216C)
Majin wrote:
is myClassList.clear() not needed at all?

That depends. std::vector::clear() simply removes all objects in the vector. The same thing can be achieved with a combination of std::vector::pop_back() calls and a loop. So I guess you could say that std::vector::clear() is the faster alternative.

Majin wrote:
does a vector not consume any memory footprint even if it only contains pointers to objects?

Pointers consume memory, so yes. Those pointers need to be placed somewhere.

Wazzak
Last edited on
I think im still confused about the different pointers.

I have a global vector with pointers to all of MyClass objects
When i create another vector with only some of the pointers to myclass which i obtained from the global vector - arent these pointers the same? So basically when i would now call clear() on the vector with pointers to a few elements - wouldnt that also automatically invalidate pointers in the global vector which contains pointers to all elements?

If that is not the case, i have to call clear() or i would have a memleak with unused poiners..?!
When i create another vector with only some of the pointers to myclass which i obtained from the global vector - arent these pointers the same?

No, they're equal, but not the same. So a copy of the pointer is destroyed when you clear() the second vector.

Just like in this example:
1
2
3
4
int x=5;
{
  int y=x;
}

y and x are equal, but they're not the same objects. y being destroyed does not affect x at all.

If that is not the case, i have to call clear() or i would have a memleak with unused poiners..?!

It doesn't matter whether you call clear() or not. clear() destroys the pointers in the vector, but destroying a pointer to an object doesn't cause the object itself to be deleted.

Or to use a different analogy: if I give you my address on a piece of paper and you destroy the note at some point, my house still won't come crashing down (good thing too).
Last edited on
I have to come in here because some of this isn't correct:

It defeats the purpose of std::vector. Why? Internally, a std::vector allocates a large block of memory. It uses the memory to store any objects that get pushed into the vector. By allocating a new object with each push, you're effectively doubling the memory used by the vector (if the vector is full). std::vector will not automatically de-allocate the memory, so your program will leak.


This isn't the case. True, vectors allocate memory for each element it contains, but it only allocates memory for the size of it's type. Pointers, no matter what they point to always have the same size, it doesn't matter if it is a pointer to an int or a pointer to MyBigComplexClass. In either case the vector allocates memory for the size of a pointer and that's it. When the vector goes out of scope it deallocates the memory that it allocated, in this case pointers, which is why you have to free the memory the pointers are referencing yourself before you clear/vector goes out of scope, otherwise you have a leak.

You are not doubling your memory if you are new-ing some big object first, then adding that address to a vector that points to that type, you are only allocating memory for the size of a pointer (as far as the vector is concerned).

For Majin -
arent these pointers the same? So basically when i would now call clear() on the vector with pointers to a few elements - wouldnt that also automatically invalidate pointers in the global vector which contains pointers to all elements?


No, you are safe doing this. Your "other" vector allocates it's own memory for the pointers, but what they actually refer to are unchanged, when your vector is cleared or goes out of scope, nothing happens to what they were referring to.

Now if you were to delete the memory that was referred to them, through some other vector or your "main" vector, the pointers themselves would be dangling.
Last edited on
Thank you all for your input - my question is now answered, the second vector uses his own memory to point to my objects, thus it will not harm any other vector that contains pointers to the same objects.

(Im aware that deleting the objects itself would make all vectors which contain still contain pointers to them, invalid (the ptr itself))
Topic archived. No new replies allowed.