tags:

views:

84

answers:

2

Hi,

I couldn't find a clear explanation, just asking to be sure; are C data types handled same way (in terms of memory management) in Obj.C? i.e. they are created on stack, released immediately etc.? So they differ from Obj.C objects? Or may we make an analogy with C# (just an analogy not exactly) so that C types are handled as 'value types' and Obj.C objects as 'reference types'?

+3  A: 

Objective-C is clean superset of C, so everything that works in C works the same in Objective-C.

Behavior of C datatypes hasn't changed. Only Objective-C objects (class instances) have their own allocation methods.

porneL
+1  A: 

Unlike C++, which actually extends C so that you can do things like "new" a char*, ObjC is, as porneL says, purely a superset and in many ways just some fancy syntactic sugar. +alloc isn't even strictly a language construct. It's just ObjC code that eventually calls malloc() and stores some stuff in an objc_object struct pointer, which you can access manually using standard C -> notation.

If you're interested in how all this works, The ObjC Runtime Programing Guide is a good place to start (though it doesn't discuss +alloc particularly, it demystifies objc_msgsend).

To your specific question, C data types are put on the stack if you do not allocate them on the heap with malloc(). ObjC objects always are allocated on the heap. But the analogy to C# is not really helpful in my opinion. Consider the following:

int x;
int *y;
NSObject *z;

"x" is what you're calling a "value type." Both y and z are what you're calling "reference types." It's not that two are C and one is ObjC; it's that two of them are pointers, and so are almost certainly pointing into dynamic memory on the heap. This is why you never have a non-pointer ObjC object ("id" is just a pointer to an objc_object struct). This is why you can use C pointer notation on ObjC objects to fetch they're ivars (you shouldn't, but you can). It's all just C.

Rob Napier