If i'm working with memory addresses spe开发者_JAVA技巧cifically, eg when writing a debugger (as opposed to working with pointers to strings, floats, etc. because it is useful to do so) what type should i be using? Ideally taking into consideration 32/64 bit considerations.
Some of the adhoc types i have been using upto now, in the same code, for the same purpose include int64_t, NSUInteger, char *, void *, intptr_t.
essentially i will need to compare and sort by address. Thanks
If you only need to compare addresses, using a void* should be the way to go. This will take any type, and the reference (&
) operator will print out a valid memory location. On top of that, it will be portable between 32 and 64 bit types, as well as random structs, etc. etc. etc.. You can compare with a regular less-than comparison, except using the address of the pointer (&
) rather than the value of the pointer itself.
If you need to reference the data in these pointers though, you're going to run into a bit of trouble...
Hope this helps.
I think it is a toss up between uintptr_t
(from <stdint.h>
) and void *
. Both will work equally well for both 32-bit and 64-bit environments. I'd probably use uintptr_t
, but you won't go far wrong with void *
either.
Another type which might come in handy is ptrdiff_t
. Just be careful.
精彩评论