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!

Recommended Answers

All 3 Replies

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.

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?

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.