Re: Storing values in dictionary with their address as the key
Re: Storing values in dictionary with their address as the key
- Subject: Re: Storing values in dictionary with their address as the key
- From: Andy Lee <email@hidden>
- Date: Mon, 28 Jul 2008 15:34:27 -0400
On Jul 28, 2008, at 2:31 PM, Dave Carrigan wrote:
On Jul 28, 2008, at 11:13 AM, Carter R. Harrison wrote:
If I wanted to store an object in a dictionary and set its key as
the object's memory address - how would I go about doing this?
I'm racking my brains trying to think of a good reason to do this
and am drawing a blank. I can, however, think of myriad bad reasons.
Count me as another mystified person -- can you say what you're trying
to do? I'm thinking maybe some kind of serialization or maybe object
caching, but nothing makes sense. It sounds like what you want is a
set of objects rather than a dictionary. If you have an address you
can just dereference the address -- you don't need to look it up in a
dictionary.
Or are you just messing around with dictionaries in general to see how
they work?
[dictionary setValue:myObject forKey:[NSString
stringWithFormat:@"%x", &myObject]];
}
The above does seem to work, but the memory address that I get
always seems to be the same.
myObject is a variable (of type pointer) that is allocated on the
stack. Its address is always going to be the same inside that tight
loop, since the stack pointer isn't changing.
In other words, you don't want the & in front of myObject, which is
giving you the address of a pointer variable rather than the value of
the pointer.
As others have already pointed out as I typed this, you can use an
NSValue instead of an NSString. But again, I can't think of a good
reason to do this.
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden