Closed TomiyokaTanaka closed 2 years ago
Interesting idea. It's just on the edge of "in scope" for this simple vector implementation. My main hesitation is that something like this is completely normal in C++ std::vector
which I am trying to model:
std::vector<char *> v;
v.push_back(strdup(s));
which will of course be a memory leak. That is, it is considered up to the user to make sure that the contents of the vector release anything they own.
That being said, I can easily imagine a second header, something like cvector_utils.h
which would have things like free_each
an other things like a cvector
aware for_each
macro to cover the common use cases like yours.
Interesting idea. It's just on the edge of "in scope" for this simple vector implementation. My main hesitation is that something like this is completely normal in C++
std::vector
which I am trying to model:std::vector<char *> v; v.push_back(strdup(s));
which will of course be a memory leak. That is, it is considered up to the user to make sure that the contents of the vector release anything they own.
That being said, I can easily imagine a second header, something like
cvector_utils.h
which would have things likefree_each
an other things like acvector
awarefor_each
macro to cover the common use cases like yours.
yeah, a second header for utilities are nice, I am gonna add that to my fork and submit another PR soon
I have just added for_each
macro and free_each
into cvector_utils.h. How is this looking?
I just called it a night, I'll check it out tomorrow.
Thanks for the PR!
It's looking great. I may do some minor tweaks to it to make it match my personal style, but I plan to merge it in ๐๐ป
@ShiromiTempest I made some minor changes post merge.
NULL
checks in cvector_free_each_and_free
since both operations it calls already do a NULL
checkOtherwise, your code was simple and functional. Thanks!
Your welcome, I am happy to help๐
This is a handy function for freeing each elements in case they are also allocated in the heap for example like string, and only using
cvector_free
won't each elements from the heapin the example above, valgrind tells that there are memory leak when using
cvector_free
instead ofcvector_free_and_free_elements
I have also added test for
make memcheck