hooleyhoop
hooleyhoop

Reputation: 9198

what type for working with/comparing memory addresses

If i'm working with memory addresses specifically, 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

Upvotes: 1

Views: 6366

Answers (3)

splicer
splicer

Reputation: 5404

Another type which might come in handy is ptrdiff_t. Just be careful.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

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.

Upvotes: 0

Ben Stott
Ben Stott

Reputation: 2218

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.

Upvotes: 3

Related Questions