views:

4778

answers:

4

I'm trying my hand at the iPhone course from Stanford on iTunes U and I'm a bit confused about pointers. In the first assignment, I tried doing something like this

NSString *processName = [[NSProcessInfo processInfo] processName];
NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier];

Which generated an error, after tinkeing around blindly, I discovered that it was the * in the NSInteger line that was causing the problem.

So I obviously don't understand what's happening. I'll explain how I think it works and perhaps someone would be kind enough to point out the flaw.

Unlike in web development, I now need to worry about memory, well, more so than in web development. So when I create a variable, it gets allocated a bit of memory somewhere (RAM I assume). Instead of passing the variable around, I pass a pointer to that bit of memory around. And pointers are declared by prefixing the variable name with *.

Assuming I'm right, what puzzles me is why don't I need to do that for NSInteger?

+9  A: 

The reason that you don't declare NSInteger with a * is because it isn't an object. An NSInteger is simply an int or a long:

#if __LP64__
typedef long NSInteger;
#else
typedef int NSInteger;
endif

If it's being used in a 32-bit application, it's a 32-bit integer, and if it's being built in a 64-bit application, it's a 64-bit integer.

Of course, you can pass an NSInteger as a pointer, but most functions simply take arguments as an NSInteger and not a pointer to it.

Objects, on the other hand, can only be passed to other functions as pointers. This is because objects have memory dynamically allocated for them, and so cannot be declared on the stack. Since an int or long has a fixed amount of memory allocated for them, this is not an issue.

Perspx
+13  A: 

NSInteger is a primitive type, which means it can be stored locally on the stack. You don't need to use a pointer to access it, but you can if you want to. The line: NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier]; returns an actual variable, not its address. To fix this, you can either remove the *, or do this:

NSInteger *processID = &[[NSProcessInfo processInfo] processIdentifier];

The ampersand is the address of operator. It sets the pointer to the NSInteger equal to the address of the integer returned, rather than the integer itself.

Dan Lorenc
I suspect that's going to be a very handy tip. Thanks.
gargantaun
That was a major gotcha for me when I moved from webdev to iPhone development. The other one to watch out for is making sure to terminate NSArray's with nil.
Dan Lorenc
If you Apple-double-click on NSInteger or other types you are confused about in XCode, you can get taken to its definition, which in this case will be:typedef int NSInteger;So it's a plain old int after all.
Mike Weller
Chuck
+2  A: 

NSInteger is just a typedef for int, AFAIK.

Andrei Tanasescu
It can be, but isn't everywhere. On 64-bit Mac OS X systems, `NSInteger` is defined as `long`. It's not hard to imagine that this might be true on an architecture running iOS someday.
Peter Hosey
+2  A: 

The * means “pointer”. The object variable holds a pointer to an object, so it has a *; the NSInteger variable holds an NSInteger, not a pointer to an NSInteger, so it does not have a *. Putting the * on that variable gives you at least a warning because you're putting an integer into a pointer variable.

Peter Hosey