5 ms·
This C code is bad for the following reasons: 1. No alloc checks 2. This module is limited to storing integers. 3. The type Vector is exposed to the user. An
by halayli 14y ago
This C code is bad for the following reasons:
1. No alloc checks
2. This module is limited to storing integers.
3. The type Vector is exposed to the user.
An alternative approach for contiguous dynamically sized array is to use mmap/madvise.
- jws 14y agoStrangely, alloc checks no longer work in Linux and possibly other OSes. Because of overcommit you can have malloc give you an non-NULL result that will fail when you try to write to it. I still include them in my code, but I don't expect they will save me.
- loeg 14y agoHowever, it is perfectly possible in Linux to turn overcommit off. So you still need to check (or segfault…).
- halayli 14y agomalloc failure isn't just bound to running out of physical memory. It can fail because of fragmentation, with or without overcommit.
- jws 14y agoSure, but I still have to design for random segfaults on memory writes. I guess I could write to all newly allocated pages and get a little more assurance that the allocation worked. I could still be whacked by a deduplicator, though unique writes would help.
- alinajaf 14y agoThanks for the feedback, > 1. No alloc checks I'm going to modify the post to do checks on alloc based on this and other comments, thanks! > 2. This module is limited to storing integers. Since this is for the most part for learning purposes, I'm not too worried about this. In the article I mention that you could maintain a list of void pointers and then cast to whatever you like. Is that the sort of thing you had in mind or could there be another way to hold data of multiple types in vanilla C? > 3. The type Vector is exposed to the user Interesting, I didn't realize you could build this library without exposing that type. If you have a moment could you elaborate on how you might achieve this? Thanks again!
- 10098 14y ago> Interesting, I didn't realize you could build this library without exposing that type. If you have a moment could you elaborate on how you might achieve this? Very simple, move the struct to the .c file. Add a forward declaration of that struct to the header file instead.