r/C_Programming • u/SeaInformation8764 • May 26 '26
Project Small and fast vector implementation
https://github.com/ephf/vectorHello, just wanted to share this small (<150 lines) header for dynamic arrays in C. Feel free to look through it and show what could be done better or ask questions. AI was not involved in the making of this.
9
Upvotes
View all comments
Show parent comment
1
u/ischickenafruit May 30 '26 edited May 30 '26
Now that I can read the code, I can see that you’re. Using vec[-1] to store your vector context. Unless you can guarantee that your context and your content are the same size and alignment (you can’t) this is UB. You’ll probably be fine on x86 but in general this is problematic.
Edit. Actually I can see you’re using a byte array to store your data. So the vector structure will be well aligned. But your content suffers from the same problem. You’ll be arbitrarily casting a byte array to your data type and hoping the alignment works out for you. On x86 unaligned access is basically free but on others you’ll land somewhere between slow and illegal.