elmedin zelenkic
elmedin zelenkic

Reputation: 3

Difference between an object and a pointer

as the title says, what is the difference between a pointer and an object.

Say I have this code..

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

and this code..

NSString *blue = [NSString stringWithFormat:@"%@"];

Is it correct to assume that they're both pointers to an object and pretty much the same? And if so, how should I think of objects in my mind ?

I do apologize if the answer exists already, I did use the search function but I've only found examples of this in the C++ language and wanted to make sure how it was in objective-c.

Upvotes: 0

Views: 1672

Answers (7)

Matt Wilding
Matt Wilding

Reputation: 20163

Pointers are a C concept, and they're identical in C, C++, and Objective-C. They are simply an integer that holds the memory address where an object is stored. In your example, both of those messages dynamically create an NSString object. Where that object is stored in your application's memory is up to the OS to decide, and you really don't care. So the OS allocates some memory and stores an instance of NSString in it. It then gives you back a pointer to that object, which you can use to reference the actual object at a later time.

Upvotes: 2

Jesse Black
Jesse Black

Reputation: 7986

An object is an instance of a class. It takes up memory while and should be released when you are finished with it. The pointer is your reference to the object.

NSString *red = [[NSString alloc]initWithString:@"red"];

and this code..

NSString *blue = [NSString stringWithString:@"blue"];

red and blue are both pointers to different objects. The importance difference here is that red is owned by you and blue is not

[[NSString alloc]initWithString:@"red"];z

returns an object that you own and have to release later

[NSString stringWithString:@"blue"]; returns an object that is not owned by you and will be released the next time the autorelease pool is emptied

These concepts are covered in The Objective-C Programming Language guide by apple (I pointed you to the specific section, its a huge document, but the section 'Objects, Classes, and Messaging' should be the most helpful to your questions)

Upvotes: 1

TeaCupApp
TeaCupApp

Reputation: 11452

In addition to Basile Starynkevitch and Bram's answer,

In objective C the difference between your code line is,

NSString *red = [[NSString alloc]initWithFormat:@"%@"];

**Above code says you own red object so it's your responsibility to release it.

NSString *blue = [NSString stringWithFormat:@"%@"];

**You don't own it someone else in your program deep down inside will own this and you don't have to release this.

I would suggest for more information reading Apple's documentation is GREAT! specially Learning, "Objective C programming guide"

Good luck!

PS : iOS 5 has new feature, memory management is done by iOS itself, Now developer can be more creative instead doing 3 grade mathematics of reference counting :)

Upvotes: 3

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15099

Also not an Objective-C expert. Here's my best guess.

Both of those types seem to be pointers. However, it looks like the difference is that the first (where you are using alloc) puts you in charge of the associated memory.

With the second type, if you instantiate the object, use it, whatever, and then it goes out of scope, the OS will likely clean it up for you. However, with the first type, you are in charge of releasing that allocated memory back to the OS.

I'm guessing that objective-C has some sort of reference counting and memory management built in to detect when that object is no longer being referenced anywhere, but the important part is that that object should persist beyond the scope of that declaration as long as you've still got a reference somewhere.

You can probably find a lot of information by reading this post: Objective-C pointers?

As far as the general definition of "object" and "pointer": Both of those types are pointers. One you are in charge the memory, and the other the OS takes responsibility for you. An object is simply defined as an instance of a class. A pointer is the memory address of that instance.

Upvotes: 0

Otiel
Otiel

Reputation: 18743

A pointer contains the address in memory where is stored the object.

              Memory address    Object
              --------------    ---------
              0
              1
              2
              3
              4
              ...
pointer ----> 10523             myObject1
              10524
              ...

Upvotes: 1

(I'm not an Objective C expert, but)

Think of objects as memory zones, a pointer refer to the zone but is not that zone.

Gross analogy: your car plate number is not your car. A number is a number (made of digits, or more abstractly an element of the set of naturals). A car is something you drive.

Upvotes: 0

Bram
Bram

Reputation: 765

i hope i can make this abit clearer.

the object is in memory. the pointer is like an arow to the memory where the object is.

see it like a directional sign to a building.. the building is the object, the directions are the pointers.

Upvotes: 0

Related Questions