0

This may be a bit of beginner type of question; however, hypothetically, if there is method that takes a pointer of class type A, how would you free to allocation from a call such as this...

void method123(A *ptr) {<stores the A pointer in a data structure>}
method123(new A());

Is there a way to free the allocation made by that 'new' without deleting the pointer itself that was stored into the data structure?

m_callens
  • 6,100
  • 8
  • 32
  • 54
  • 1
    If you store the `new A()` in a data structure, then you shouldn't be deleting it, otherwise the pointer in your data structure would be invalid. – Colonel Thirty Two Oct 13 '15 at 19:05
  • @ColonelThirtyTwo Yes, that is my issue. If I don't delete it however, I have a memory leak print from valgrind. – m_callens Oct 13 '15 at 19:08
  • as a beginner, actually you should rarely need to deal with tricky aspects of memory allocation. Just **dont** use pointers or heap allocation whenever possible – 463035818_is_not_an_ai Oct 13 '15 at 19:08
  • @tobi303 Well this is a situation that arose in a project for school that I'm working on and its causing memory leaks, so I DO have to deal with it. I myself am not a beginner, I've been doing c++ for a couple years. – m_callens Oct 13 '15 at 19:10
  • Either the data structure itself needs to delete it at the appropriate time, you yourself need to free it later in your program after you know it's no longer used, or use smart pointers which do reference counting for you. – Colonel Thirty Two Oct 13 '15 at 19:11
  • @m_callens then why dont you post your actual problem? – 463035818_is_not_an_ai Oct 13 '15 at 19:11
  • @tobi303 well this is the exact situation. There are pointers being stored in a queue of pointers and they are instantiated using data from a data file using 'new'. I can't delete the pointer or it will invalidate it in the queue, but if I don't by some means there is a memory leak. the project itself is 12 classes so its too big to post. – m_callens Oct 13 '15 at 19:14
  • do you really have to use heap allocation (via `new`)? If you dont, there is no memory leak – 463035818_is_not_an_ai Oct 13 '15 at 19:16
  • @m_callens In general you aren't supposed to use `new` and manage dynamic allocation yourself in c++. There are rare use cases, but yours isn't one. – πάντα ῥεῖ Oct 13 '15 at 19:17
  • @tobi303 So there is a base class and 5 derived classes. The queue to be populated is of type base, however, we are expected to use polymorphism to push derived instances to the queue of base type. Unless there is a way to do inheritance that I don't know... – m_callens Oct 13 '15 at 19:21
  • @m_callens Use smart pointers as mentioned in the duplicates answers, but not `new`! – πάντα ῥεῖ Oct 13 '15 at 19:27
  • @m_callen I have to admit, that I also didnt fully understand how to deal with this (polymorphism but avoiding pointers) and I am just about to decide on a strategy. At the moment my guess would be to write a wrapper class that holds a pointer to the base class and then you can create objects of this class on the stack (without `new`) that nicely handles resources in cstr/dstr so that you dont have to care about it "manually" anyomore. And actually I think this is the concept of smart pointers (but I am still a bit confused about them..) – 463035818_is_not_an_ai Oct 13 '15 at 19:27

0 Answers0