3 ms·
Nitpicks: * Typo in the header file (SIZE versus CAPACITY). * No error checking on malloc() or realloc() return. * Out of bounds access is a programmer error
by klodolph 14y ago
Nitpicks:
* Typo in the header file (SIZE versus CAPACITY).
* No error checking on malloc() or realloc() return.
* Out of bounds access is a programmer error, so you should call abort() and not exit(). Alternatively, assert() is fine too.
* Potential integer overflow for capacity field (size field can't overflow because capacity overflows first). On a 64-bit non-ILP64 system, resizing to a capacity of 2^31 elements will request a buffer of 0xfffffffe00000000 bytes, assuming sizeof(int) is 4.
* A 32-bit version could theoretically get an overflow, but only if a 2^31 byte allocation is successful... the next larger allocation will be for 0 bytes.
Edit: As someone pointed out in the comments, arr[271] is the 272nd element, whereas the article claims it is the 270th element.
Further notes:
If you end up implementing a lot of these, invest in a consistent naming scheme for the fields. For example, map, mapsz, mapalloc; then data, datasz, dataalloc.
You can also try writing generics in C but I strongly recommend just switching to C++ (or some other language) if the boilerplate code bothers you. I believe that std::vector was the biggest reason for GCC's switch to C++ internally.
- alinajaf 14y agoArticle author here, thanks for going to the trouble to highlight these. Really appreciate the feedback and will edit.
- fruchtose 14y agoI've read that malloc(3) will not always return errors when you expect them to [1]. Any comment on this? [1] http://news.ycombinator.com/item?id=4735589 http://news.ycombinator.com/item?id=4735589
- Someone 14y agoIt is unlikely the world will change, so adjust your expectations. Also keep checking for error returns according to the API, though, as that is a 'not always'.
- klodolph 14y agoI expect it to fail when it runs out of address space, or when address space fragmentation prevents it from fulfilling the request. Isn't that exactly when it fails?