Newbee
Newbee

Reputation: 3301

What is the difference between C pointer and Objective C pointer?

I know its a very basic question. Got some memory issues I need to clarify. Here is my doubt:

int *p = malloc (50); // will allocate 50 bytes and it is pointed by p.

// Freeing C pointer-->

free(p);

Objective-C pointers:

ClassAobject *objA = .... // allocated ClassAobject..

// Freeing obj-C pointer--->

objA = nil // Is it enough??? will it release all ivars memory properly..

what if the case, If I have some C pointers inside the objective C class? How to handle this in ARC

Upvotes: 2

Views: 341

Answers (2)

gnasher729
gnasher729

Reputation: 52592

There is no difference between "C pointers" and "Objective-C pointers". void* and int* are exactly the same in C and Objective-C.

However, when you use ARC some pointer types are handled specially by ARC. These pointer types are:

id
Pointers to Objective-C objects like NSString*
Class
Blocks

These are all pointers to various kinds of Objective-C objects that are reference counted. ARC automatically keeps track of how many references to any of these objects exist, and if the last reference is removed, the object itself is deallocated.

If an object is deallocated, ARC automatically removes all the references to objects that it referred to. However, if an object has pointers that are not under ARC control, you have to do whatever you need to do in dealloc.

Upvotes: 0

user529758
user529758

Reputation:

The title doesn't reflect what you're asking. There's no difference between C pointers and "Objective-C pointers". Really they're just plain ol' C pointers.

What you're asking for is the difference between their correct usage. If a pointer points to an Objective-C object, then under MRC, you have to do

[obj release];

to decrease its reference count (which can potentially deallocate it). Under ARC, setting the pointer to nil is enough (as in your example).

Upvotes: 10

Related Questions