c - Does malloc() have a maximum return value? -
does size_t
value of virtual memory pointer returned malloc()
have upper boundary?
i wondering whether can safely set significant bit of 64 bits pointer indicate not pointer literal integer instead.
as @datenwolf's answer states, can't make assumptions how malloc
providing memory address. msb may contain important bits overwrite, if attempted use them store meta data. have worked on 32-bit system returned addresses bits set in msb of addresses (not malloc
, other system specific memory allocation functions).
however, is guaranteed malloc
return address suitably aligned system. example, on 32-bit system, you'll 4-byte aligned pointer, , on 64-bit, you'll 8-byte aligned pointer. means guaranteed lower 2 or 3 bits respectively will zero. increase number of guaranteed bits using memalign
instead. same effect storing meta data in significant bit. get/set literal, can up/down shift remaining bits.
however, wouldn't suggest either method. save heartache, , allocate little more memory store flag. unless you've got billions of them, it's not worth it.
Comments
Post a Comment