What is the difference between int *p; and ClassA *Obj? - objective-c

Could anybody explain me the difference between the pointer concept when using primitive datatypes like int and Objective-C data types like NSString?
thanks,
bala

Pointers are just that. They locate a memory location. You de-reference that pointer(location) to either primitive data type or user defined types as appropriate. I don't see a difference b/w pointers to user defined and primitive data types.

Assuming both pointer had been initialised properly dereferencing the former gives you an int, whereas doing so to the latter gives you a ClassA.
So in fact there is no difference, but the data instance those pointers are referring to.

Related

Why are some properties and variables in Objective C initialized with a pointer while others are not?

Coming from Java et al, I'm not clear on the difference between these two declarations:
#property (nonatomic, readwrite) NSInteger score;
#property (nonatomic, strong) NSMutableArray *cards;
Why is the pointer, *, not a requirement on both property declarations?
I've seen this a lot in local variables too:
- (void)viewDidLoad
{
[super viewDidLoad];
int foo = 1;
NSString *bar = #"foo";
}
What's the difference between static allocation of primitive type int and NS types?
Objective-C objects are always allocated on the heap, so you always access them through pointers. Variables of primitive (or struct) types can be, and typically are, allocated on the stack and accessed without pointers.
If you're familiar with Java, it's basically the same semantics. Primitive types are accessed and passed around by value, objects by reference. The difference is that ObjC has (by virtue of its C lineage) syntax explicitly marking that difference.
Type names that start with an uppercase prefix in Apple frameworks aren't all ObjC classes. NSInteger is a primitive type just like int, so you can and usually do use it without pointers.
pointer is always used for referring to something at the heap but not when you referring to something on the stack.But
for some primitive types and simple structure which are accessed via the stack so you don't need to use pointer..
NSInteger is a primitive type, that means it will be stored locally on the stack. there is no need to use a pointer to access it, but if you want to use pointer then you can.
You can have a pointer to an NSInteger if you really want to with following way:
NSInteger *pointerToProcessID = &yourNsintegervar;
If you look at the definition of NSInteger you'll see that it is a typedef for a simple integer. Basically, all the non-object types are stored as simple values, while the types that are complex objects are typically pointer properties. There are a couple reasons why these more complex objects are stored as pointers:
Using the value, itself, instead of the pointer would require copying (that is, if you use a pointer, you can put the object somewhere else and you only need to copy the much shorter address rather than all of the content that happens to be in that object, and hence it is more efficient that way).
When using a non-pointer type, it is necessary to know the required storage space, which works if you know the exact type of the object, but fails in the case of inheritance (e.g. an NSMutableArray may add additional fields to NSArray, for example. If you were to use NSArray instead of NSArray*, then assigning from an NSMutableArray would be broken, because the system would only have set aside enough space for the fields in the base class and not for the derived class. When using a pointer, however, since the pointer size is the same for both the base and derived types, one can assign the pointer for a derived type to a pointer to the base type, and still have things work correctly).
Note that it is possible and safe to use a pointer type with these primitive types, as well; however, this is not done for efficiency reasons (it would create additional allocation and dereferencing where not necessary).

What's the difference between (ReturnClass *)methodName... and (ReturnClass)methodName... in Objective-C?

Quick question: The difference between (ReturnClass *)methodName... and (ReturnClass)methodName... in Objective-C?
I know (ReturnType *)methodName... returns an instance of ReturnClass as opposed to (ReturnClass)methodName just returning what?
Thank you.
if your ReturnClass is an object (which is most likely) then you can't use it without *. You'll get a compilation error. Otherwise *is a pointer to instance (primitive type or struct), and absence of * is instance itself
EDIT: Brilliant explanation on pointers from Joe:
Think of the instance as your house and the pointer as your address. Your house is a large object, so instead of making copies of your house and passing it to your friends when you have a party you give them your address so they can get to your house. Now if you had a small object, like a flyer, it would be easy to just make copies and pass out rather than give everyone instructions to get to that piece of paper. Think of the large object (house) as an Objective-C class or struct full of info, and the small object (flyer) as a primitive like int, BOOL, double etc

Why does an object variable have to be a pointer?

If I declare an NSMutableString
NSMutableString *str_value;
Why do I have to declare this as a pointer (using *)? If I don't, I get a compilation error.
Could someone explain this clearly?
Recall that Objective C is a superset of C. When you declare a variable without * in C, it is an indication that the memory for that variable is allocated either in the automatic storage if it is a local variable, as part of its outer structure if it is a member of a structure, or in the static memory if it is a static or a global. Using the typename or a structure tag without * in a parameter list of a function indicates passing by value.
The designers of the Objective C language could have taken the Java-like route, making every class instance is a pointer without the pointer syntax, but then the readers of programs in Objective C would need to know if a name represents a typedef based on a struct or an id type to answer even the most basic questions about objects of that type, such as if it is implicitly passed by pointer or by value, if it is allocated as part of the owning structure or as a heap object pointed to by a pointer inside the structure, and so on.
To avoid this kind of confusion, designers of Objective C decided to preserve the explicit pointer syntax for id objects.
A pointer means you are pointing / referencing to that class. Yes it will cause a compilation error, the reason for a pointer is mainly for memory. One data type (int or BOOL or float etc...) is only a few bytes, therefore it is not necessary to have a pointer. But NSMutableString and other Objective-C classes have a lot of properties and methods, with a lot of code. Therefore, since in your apps will have a lot of objects, which will use a lot of memory and thus slow down your app/decrease performance. Of course you should release the object once you make a pointer.

Usage of "id" in Objective-C?

I have a function that returns "id". Does this include a return of void? (as in nothing) Or does "id" require some kind of object/variable?
In Objective-C, id is a keyword that represents an untyped object pointer. It's kinda like void*, the untyped pointer, but it adds the restriction that the pointer must point to some sort of Objective-C object.
id is a general data type that can wrap most objects. If you can, you usually want to opt for coding specific data types, but if the situation (in a method for example) can use a wide range of data types, id is used.
This Stackoverflow post should help to explain what it is and when to use it. A simple Google search will also turn up information.
In Objective C, id means object of any type, akin to void* in C/C++. You can return nil for from a function returning id to indicate that you do not want to return anything in particular.
'id' is a pointer to an instance of an Objective-C class. So your method can return a pointer to an instance, or 'nil' (a zero pointer).

Objective-C use of pointers

When do you and when dont you need the * symbol (which is because in objective-c all variables like NSString are pointer variables)?
For example when do you need to do "NSString *" instead of just "NSString"?
In Objective-C, all object references are pointers, so you always need the pointer operator when you declare with an Objective-C object.
For other types, the use is exactly the same as in C. Use pointers when you want to pass data structures or primitive types by reference.
You use the asterisk for all Objective-C objects (such as NSDictionary, NSString, NSNumber).
For anything that is a primitive type (int, double, float) you don't need the asterisk. However, the NS prefix doesn't always mean that you must use an asterisk. Cocoa defines some structures (such as NSInteger, NSRect, NSPoint) that are are based on primitive types. You don't use the asterisk here either. An NSRect, for example, is just a structure of an NSPoint and NSSize, both of which are made up of 2 CGFloats (a primitive type).
You can pass a pointer to one of these primitive types or structures using the * notation.