eteran / c-vector

A dynamic array implementation in C similar to the one found in standard C++
MIT License
737 stars 109 forks source link

Add vector destructor #45

Closed dm9pZCAq closed 1 year ago

dm9pZCAq commented 1 year ago

I propose adding a vector destructor that would be called by cvector_free.

For example, consider the scenario where I read a file and store pointers to the beginning of each line in the vector (with newline characters \n replaced with null bytes \0).

The file data would look something like this:

+----------------------------------------+
|line 1\nline 2\nline 3\nline 4\nline 5\n|
+----------------------------------------+
 ^       ^       ^       ^       ^
 vec[0]  vec[1]  vec[2]  vec[3]  vec[4]

With the addition of a vector destructor, I would be able to free all the data by using the first pointer (vec[0]). This would provide a cleaner and more efficient way to manage memory when using cvector.

eteran commented 1 year ago

There already is a destructor macro, called cvector_elem_destructor. And if you look at the cvector_free macro, it actually will call it on each element before freeing the vector itself.

So I'm not sure that what your proposing would add anything not already there, please re-open if I'm mistaken.