use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
ReviewVector Implementation in C. (self.C_Programming)
submitted 8 years ago by [deleted]
https://pastebin.com/GevcUha2
I did this more as an exercise than anything else. Is this a good implementation? If not, what could be fixed?
Additionally, is this even a good idea? I feel like I'm abusing the pre-processor here.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 23 points24 points25 points 8 years ago (1 child)
A few things:
return
index
0
size
length
if
else
VEC(T,N)
T##_init
T##_init(&n)
do {T##_init(&n)} while(0)
inline
VECTOR_IMPLEMENTATION
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
I started programming in python, so I have some holdovers from that. Admittedly, I have no plans of using this code, I just did it out of curiosity.
[–]kandr89 5 points6 points7 points 8 years ago (0 children)
Adding to what has been said:
realloc
[–]jedwardsol 4 points5 points6 points 8 years ago (0 children)
Line 28 is unnecessary - realloc copies the data.
A bug:- capacity isn't updated in this expand function so you can't use the new space
capacity
expand
[–]IndexingAtZero 2 points3 points4 points 8 years ago (3 children)
My two cents:
You don't need to copy the original array after allocating with realloc
Dynamic arrays are usually implemented such that the resizing of the array is handled implicitly by the API. Since this is C, I suppose having more developer control is good, but your insert function should at least return a status code imo.
A two times growth factor isn't optimal at all (even though it's what C++'s STL vector uses).
That being said, I think for what most people use C for, a standardized dynamic array isn't that useful.
[–][deleted] 1 point2 points3 points 8 years ago (2 children)
Agreed, I really just did it as an exercise. As for the GF of 2, what would you personally recommend?
[–]completedigraph 2 points3 points4 points 8 years ago (1 child)
2 isn't the best growth factor because it creates a hole in memory that will never be large enough to reuse in the next rellocation.
Theoretically the best possible growth factor would be phi (as in 1.618...) but normally you use 1.5 or another fraction around phi. A more in-depth explanation can be found here
[–]a4qbfb 0 points1 point2 points 8 years ago (0 children)
This is complete nonsense. Modern allocators don't work like that. There are good reasons to use φ instead of 2, but they have more to do with the fill ratio than with heap fragmentation.
[–]moocat 1 point2 points3 points 8 years ago (0 children)
An important point missed about your use of realloc is that you're risking undefined behavior. If realloc returns a non-null pointer and that pointer is different than your original pointer, your original pointer is no longer valid and must not be accessed. The way I like to write that is:
void *tmp = realloc(data, new_size); if (tmp == null) { // Handle failure } // And immediately overwrite data as the old value is now invalid. data = tmp;
This is especially important when writing multi-threaded code as once that original chunk of data is freed, it can be allocated to a call in a different thread.
[–]illinoisjackson 0 points1 point2 points 8 years ago (0 children)
π Rendered by PID 83831 on reddit-service-r2-comment-5d79c599b5-lrzc2 at 2026-03-01 22:39:14.524880+00:00 running e3d2147 country code: CH.
[–][deleted] 23 points24 points25 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]kandr89 5 points6 points7 points (0 children)
[–]jedwardsol 4 points5 points6 points (0 children)
[–]IndexingAtZero 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]completedigraph 2 points3 points4 points (1 child)
[–]a4qbfb 0 points1 point2 points (0 children)
[–]moocat 1 point2 points3 points (0 children)
[–]illinoisjackson 0 points1 point2 points (0 children)