What type should I use to count objects in C?

You often need to count objects in memory: for example, if you have a linked list, and you want to keep or calculate the length. What datatype should you use for this?

The right answer is uintptr_t: the unsigned integral type which is guaranteed to be able to hold a pointer. Why? Because the worst case for “number of objects in memory” is that each object is a single byte, and they are packed into the entire address space. How many bytes are there in the address space? It’s exactly the largest possible pointer value. The smallest type capable of storing “the largest possible pointer value” is of course a pointer. But using a pointer as a counter is unconventional: we would be doing arithmetic with it. Instead, we use uintptr_t, the integer value of the size as a pointer.

Tagged #c, #programming.

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2016. Content is not associated with my employer. Found an error? Edit this page.