0

In this snippet:

NSString *testString; 
testString = [[NSString alloc] init]; 

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

After the first line, what is *testString and what is testString?

4 Answers 4

2

The first line you are creating the pointer of NSString type. Pointers in C++ and Objective-C are denoted by the asterisk (*) character when you declare them. The second line you are saying this pointer called "testString" references the memory location of the NSString object that you have allocated in memory.

Sign up to request clarification or add additional context in comments.

Comments

2

All objects are referred to by pointers. The first line

NSString * testString;

declares the instance variable. If your variable type is an object (aside from type id), you need the *

from then on the reference to testString is pointer

If you create 2 strings. 2 physical objects are created (in memory)

NSString * testString = [[NSString alloc] init];
NSString * testString2 = [[NSString alloc] init];

//setting testString to testString2 will lose the pointer to testString for good

testString = testString2;  //<--bad if you still care about testString (and leaks the memory too)

I recommend checking out Apple's guide on Objective-C. Specifically this section

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1

Comments

1

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

The init method returns a generic pointer to an object -- its return type is id. testString is a pointer to an NSString, which is an object, so you are assigning a pointer to another pointer. Dereferencing the assigned-to pointer would be a type mismatch.

A variable name is a place (a label for a memory address) in which to put something. The type of the variable is the kind of thing that you can put there. In the case of a pointer, the kind of thing that you put in it is also a memory address. In order to get that address, you dereference the pointer. The kind of thing that you can put at that address is different from the kind that you put in the pointer itself.

After the first line, what is *testString and what is testString?

After the first line, *testString, or the thing at which testString points, is garbage (actually undefined). testString is a pointer (4 or 8 bytes depending on your system) to a address in memory, and it is also undefined.

After the second line, *testString is an NSString object. testString is still a pointer to an address, where there is a valid NSString object.

Comments

1

That's simply because we affect the pointer.

[[NSString alloc] init] returns a pointer to an NSString.

In Cocoa every object is dynamically allocated (as in malloc in C) and every NSObject is manipulated thru its pointer/address (in such a point that many ObjC programmer don't even know that they are manipulating pointers and not objects)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.