0

Sorry if my last question is very similar, but I have another query of when and if I should use the delete keyword.

Example code.

I initialize a class that has pointer to vector of pointers as their arguments. These objects are assigned memory from the heap. I then pass these objects to my class constructor which is also assigned memory from the heap.

Here is the code: in the main function

     //This will also change each loop
     vector<A*>* ex1 = new vector<A*>;
     vector<B*>* ex2 = new vector<B*>;         
     vector<C*>* ex3 = new vector<B*>;

     for(int i =0; i < 10; i++){

       ex1->push_back( new A(i); );
       ex2->push_back( new B(i); );
       ex3->push_back( new C(i); );

     }


     MainClass* ex_main = new MainClass(ex1,ex2,ex3);

inside MainClass.cxx

   MainClass::MainClass(vector<A*>* ex_A, vector<B*>* ex_B, vector<C*>* ex_C): m_ex_A(ex_A), m_ex_B(ex_B), m_ex_C(ex_C) {}

inside MainClass.h

   vector<A*>* m_ex_A;
   vector<B*>* m_ex_B;         
   vector<C*>* m_ex_C;

m_ex1, m_ex2 and m_ex3 have been assigned to the stack, but have been assigned to the pointer to vector of pointers.

Questions.

Do I delete the m_ex_A,B ans C in the destructor of class MainClass (I know I will need to delete the elements in each vector as well) and also the pointer to vector of pointers in the main function?

e.x. in destructor of MainClass (I do something a bit more generic than this, but this is quicker to show )

for(int i = 0; i < m_ex_A->size(); i++){
     delete m_ex_A->at(i);
 }

  delete m_ex_A;

I would then do a similar approach for pointer to vector of pointers ex1, ex2, ex3 in the main function and also delete the main class.

My confusion comes from the fact that m_ex_A,B and C are assigned to the Stack and not dynamically, but they get assigned to dynamic objects?

My guess would be to use delete to m_ex_A,B, C objects as I have dynamically initialized them?

Also what is more efficient using heap memory or stack??

I think I might be using dynamic memory to much...

3
  • 3
    Using stack will certanly be more efficient performance wise. Don't use dynamic allocation if you don't really need to. Commented Nov 24, 2011 at 12:04
  • 1
    you are mixing 'Vector' and 'vector'. Are they supposed to be different, or is this a Typo? Is vector supposed to be 'std::vector'? Commented Nov 24, 2011 at 12:06
  • 1
    @FabioFracassi sorry it is a typo Commented Nov 24, 2011 at 12:06

2 Answers 2

6

You definitely should delete your dynamically allocated objects somewhere. Could as well do it it the destructor of MainClass, but personally, I'd consider it a bad style. Why? Because you're allocating and deleting in different places, in different entities. In fact, you're transferring ownership of these objects, so you should be very careful to not to delete twice or something. I'd rather use shared_ptr or unique_ptr and releave my mind of all this micromanagement. You can search those on the web, and they are extremely easy to use.

As for the second question - you MUST only delete every object once. Copying pointers doesn't create new object, it's just another pointer to the same object.

Sign up to request clarification or add additional context in comments.

4 Comments

you have kind of half answered my question. If I delete in the class do I also want to delete them in the main function? I am confused here as the object from the stack points to the address of the object from the heap?
No, you delete it in one place only! If you delete it twice that is UB (Undefined Behavior) which is a place you don't want to visit
@FabioFracassi So this UB is because I have already de-allocated the memory. I think this is clear in my head now. It makes sense to me when I think of it changing ownership when I pass it to the class.
@MWright What you do in MainClass' constructor is called shallow copy. You passed the pointers and not the memory those pointers copy to. You need to make sure you only delete that memory once (preferably in the same entity you allocated it in) and that you're not dereferencing those pointers once the memory they point to has been deleted.
3

This is not a direct Answer to the question but trying to clear up the underlying issue:

The conceptual issue is that raw pointers do not convey (and worse have no way of) ownership information. They are just a pointer to a memory location.

Now, whenever you allocate dynamic memory with new you are responsible to delete it somewhere. To decide where this somewhere is is part of designing your program. If you pass a pointer to some object like in your example you need to document if the object takes ownership of the passed in pointed to memory (in this case the object becomes responsible for cleanup) or if it does not (and the caller remains responsible).

With raw pointers there is no way of knowing (except for documentation or examining the implementation) whether or not any given function takes ownership.

This is where the new C++11 (also available from boost) smart pointers (unique_ptr and shared_ptr) fit in. They immediately convey who is responsible for deleting the memory and even better take care of doing so automatically (and with little- or in case of unique_ptr no- overhead)

So as a final guideline: Never ever use raw pointers instead of smart pointers or some other kind of RAII mechanism! (until you gain much more experience with those and can prove that raw pointers are in fact a better solution in that special case)

3 Comments

If the Class or Function takes ownership of the raw pointer and I delete the raw pointer outside of these Classes and Functions. Will it still free up memory (I know there is no way of knowing who has ownership) ?
I do not understand your question. When you do delete some_ptr for the first time the pointed to memory will be released. Anyone else who still has a copy of the pointer (to the now released memory) will now encounter UB if the pointer is used (i.e. dereferenced or deleted). So if your object takes ownership (i.e. uses the pointed-to memory) and you delete it outside, it will be deleted and probably cause havoc inside your object.
Thank you for your help. I think it has finally sank in :-p

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.