Pointers to NSNumber data

Mikuro

Crotchety UI Nitpicker
Hi. I need some advice on how to get pointers to numbers stored in an NSNumber, which in turn is stored in an NSDictionary (not that that matters, I guess). I'm not sure of a good, reliable way to do it. It seems that "(x+2)", where x is a pointer to an NSNumber, works, but...it seems awfully un-Cocoa-ish.

So, do you think this is a safe way to do it? And is there nothing stopping me from "manually" manipulating the value of the NSNumber like so:
*((unsigned int*)x+2)=2345;

It seems to me like if it were that simple, there'd be NORMAL ways to do it. Am I setting myself up for disaster by trying to go low-level on Cocoa objects?
 
You're trying to store a pointer to a number into an NSNumber object?

Since NSDictionaries can store any object, why not create a simple class specifically designed for storing pointers of numbers. It's not clean and smart to go about hacking Cocoa's classes and such...

For example (warning - typed in Safari):
Code:
@interface PointerHolder {
int *ptr;
}
- (void)setPointer:(int *)pointer;
- (int *)pointer;
@end

@implementation PointerHolder

- (void)setPointer:(int *)pointer
{
    ptr = pointer;
}

- (int *)pointer
{
    return ptr;
}

@end
 
Oh wait hehe. I think I misunderstood your post.

You are wanting to get a pointer to the value inside an NSNumber? The cleanest way to do this is to subclass NSNumber and return pointers to its values. For example:

Code:
@interface PtrNumber : NSNumber {
- (int *)numberPointer;
@end;

@implementation PtrNumber
- (int *)numberPointer
{
    int value = [self intValue];
    return &value;
}
@end

Edit ok that won't work because that won't update the value inside the NSNumber. If I were you I'd use my PointerHolder class above to do what you want. NSNumber isn't the way to go for this really... :eek:
 
The code with PtrNumber isn't going to be safe either. The reason is you're returning the address of value, which is a local variable defined within the method - (int *)numberPointer. If Objective-C is anything like C/C++, then the same rules apply. That means pointing to a variable that is out of scope will lead to undefined behavior (and boy, do I hate this phrase...). In short, don't do it.

As for Mikuro's way of accessing the number element, don't do it either. Not only is it un-Cocoa like, it is absolutely unsafe. From that code, you're assuming that the variable that holds the number is 8 bytes (2 * sizeof(unsigned int *) = 8 bytes) in from the start of the NSNumber variable's memory address. This is a dangerous assumption to make, since on a 64 bit machine, (unsigned int*)x+2 will move it 16 bytes, since the size of a pointer is now 8 bytes. This also may fail on 32 bit systems depending on the compiler flags used, as they determine the 'packing' a structure/class gets.

In conclusion, use kainjow's PointerHolder class. That's the safest thing to do :)
 
Thanks, both of you. And that makes perfect sense, Viro. Glad I asked.

I guess there really is no reason I can't make my own class with one variable, and then just make some accessors to return the pointer, like in the PointerHolder class (except that I don't want it to store a pointer, I just want to get the pointer to the value it stores; but still, not a problem). Then I can manipulate the data directly from anywhere by using that pointer. There's nothing wrong with that, is there? (I mean, aside from the fact that it goes against object-oriented design. But that doesn't really matter in this case.)

All I really want is a dictionary of unsigned ints. Preferably one I don't need to code myself from the ground up. ;)
 
Heh it'd take you all but 2 minutes to code a class that holds an unsigned int and returns a pointer to its value.. ;)
 
Oh, I know. :) When I said "preferably one I don't need to code myself", I meant that I'd like to piggyback on the functionality of NSDictionary, which this will let me do. The only problem is that objects will take a lot more memory than raw data, but I can live with that, at least for now.

But while we're on the topic, can anyone recommend a good C library for dealing with hash tables for generic data types?
 
Back
Top