Objective-C: difference between id and void *

Objective CVoid Pointers

Objective C Problem Overview


What is the difference between id and void *?

Objective C Solutions


Solution 1 - Objective C

void * means "a reference to some random chunk o' memory with untyped/unknown contents"

id means "a reference to some random Objective-C object of unknown class"

There are additional semantic differences:

  • Under GC Only or GC Supported modes, the compiler will emit write barriers for references of type id, but not for type void *. When declaring structures, this can be a critical difference. Declaring iVars like void *_superPrivateDoNotTouch; will cause premature reaping of objects if _superPrivateDoNotTouch is actually an object. Don't do that.

  • attempting to invoke a method on a reference of void * type will barf up a compiler warning.

  • attempting to invoke a method on an id type will only warn if the method being called has not been declared in any of the @interface declarations seen by the compiler.

Thus, one should never refer to an object as a void *. Similarly, one should avoid using an id typed variable to refer to an object. Use the most specific class typed reference you can. Even NSObject * is better than id because the compiler can, at the least, provide better validation of method invocations against that reference.

The one common and valid use of void * is as an opaque data reference that is passed through some other API.

Consider the sortedArrayUsingFunction: context: method of NSArray:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;

The sorting function would be declared as:

NSInteger mySortFunc(id left, id right, void *context) { ...; }

In this case, the NSArray merely passes whatever you pass in as the context argument to the method through as the context argument. It is an opaque hunk of pointer sized data, as far as NSArray is concerned, and you are free to use it for whatever purpose you want.

Without a closure type feature in the language, this is the only way to carry along a hunk of data with a function. Example; if you wanted mySortFunc() to conditionally sort as case sensitive or case insensitive, while also still being thread-safe, you would pass the is-case-sensitive indicator in the context, likely casting on the way in and way out.

Fragile and error prone, but the only way.

Blocks solve this -- Blocks are closures for C. They are available in Clang -- http://llvm.org/ and are pervasive in Snow Leopard (http://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/GCD_libdispatch_Ref.pdf).

Solution 2 - Objective C

id is a pointer to an objective C object, where as void* is a pointer to anything.

id also turns off warnings related to calling unknown mthods, so for example:

[(id)obj doSomethingWeirdYouveNeverHeardOf];

will not give the usual warning about unknown methods. It will, of course, raise an exception at run time unless obj is nil or really does implement that method.

Often you should use NSObject* or id<NSObject> in preference to id, which at least confirms that the object returned is a Cocoa object, so you can safely use methods like retain/release/autorelease on it.

Solution 3 - Objective C

If a method has a return type of id you may return any Objective-C object.

void means, the method won't return anything.

void * is just a pointer. You won't be able to edit the content on the address the pointer points to.

Solution 4 - Objective C

id is a pointer to an Objective-C object. void * is a pointer to anything. You could use void * instead of id, but it's not recommended because you'd never get compiler warnings for anything.

You may want to see stackoverflow.com/questions/466777/whats-the-difference-between-declaring-a-variable-id-and-nsobject and unixjunkie.blogspot.com/2008/03/id-vs-nsobject-vs-id.html.

Solution 5 - Objective C

/// Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

/// A pointer to an instance of a class.
typedef struct objc_object *id;

The above code is from objc.h, so looks like id is an instance of objc_object struct and isa pointer can bind with any Objective C Class object, while void* is just an untyped pointer.

Solution 6 - Objective C

My understanding is that id represents a pointer to an object while void * can point to anything really, as long as you then cast it to the type you want to use it as

Solution 7 - Objective C

In addition to what's already said, there's a difference between objects and pointers related to collections. For example, if you want to put something into NSArray, you need an object (of type "id"), and you can't use a raw data pointer there (of type "void *"). You can use [NSValue valueWithPointer:rawData] to convert void *rawDdata to the "id" type for using it inside a collection. In general "id" is more flexible and has more semantics related to objects attached to it. There's more examples explaining id type of Objective C here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmk12View Question on Stackoverflow
Solution 1 - Objective CbbumView Answer on Stackoverflow
Solution 2 - Objective CPeter N LewisView Answer on Stackoverflow
Solution 3 - Objective CfphilipeView Answer on Stackoverflow
Solution 4 - Objective CMichaelView Answer on Stackoverflow
Solution 5 - Objective CjackView Answer on Stackoverflow
Solution 6 - Objective ChhafezView Answer on Stackoverflow
Solution 7 - Objective CbattlmonstrView Answer on Stackoverflow