0

If in a class method I pass in a list:

std::list< regmatch_t* >& pm_list;

and dynamically allocate memory for an array (of type regmatch_t) which is then added to the list as a pointer:

regmatch_t* pm = new regmatch_t[ num_subexpressions ];
pm_list.push_back( pm );

will the list destructor automatically deallocate the pointer/array, or will this become a memory leak for whomever uses this method without explicitly deallocating each regmatch_t* pointer?

Thanks!

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Narue
0

If you don't explicitly delete your memory, it doesn't get deleted.

>will this become a memory leak for whomever
Yes, yes it will.

0

Thanks for the response...

Would it be considered bad form then to create a list pointers in this manner (as a parameter to a method)... or is it reasonable to expect the next programmer to responsibly deallocate the pointers in the list?

0

>Would it be considered bad form then to create a list pointers in this manner
Yeppers. A better solution is to wrap the list in your own object with a destructor that guarantees the memory is released. Or you can use a smart pointer that works well with standard containers.

>is it reasonable to expect the next programmer to
>responsibly deallocate the pointers in the list?
Unless they can lazy at the same time, don't expect programmers to be responsible.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.