K
Keith Thompson
On Jun 27, 11:15 am, (e-mail address removed) (Richard Tobin) wrote: [...]I don't think there's guaranteed to be any type that can count
all the allocated objects, though uintptr_t must work if it exists.
uintptr_t is not relevant to counting successful allocations.
It could be that malloc() always succeeds.
Usually there's something else that puts an upper bound.
For example, when you count allocations, you count allocations for /
something/, for example, lines in your text editor.
You can simply put the 'unsigned long' upper bound which is at least
2^32-1, sufficient practically for any text file.
The number of distinct objects cannot exceed 2**N, where N is the size
in bits of type void*.
uintptr_t, if it exists, can represent a distinct value for each
possible void* value. Therefore, uintptr_t, if it exists, can safely
be used as an object count.
size_t cannot, in general, because it's only guaranteed to be big
enough to represent the size in bytes of a single object (though
calloc() introduces some confusion here). For example, a system with
segmented addresses might have 64-bit pointers, but only allow
2**32-byte objects; size_t would only have to be 32 bits, but
uintptr_t would have to be 64 bits.
(Most modern real-world systems have flat address spaces, so size_t is
big enough to cover the entire address space, but it's not
guaranteed.)