On Sat, Sep 22, 2012, at 04:46 PM, Boris Dobroslav wrote: > Hi, > > I'm perplexed by one line that appears in the file AVSPDocument.h from > the apple example code project AVSimplePlayer: > > staticvoid *AVSPPlayerItemStatusContext = &AVSPPlayerItemStatusContext; > > This definition appears outside of Objective-C @interface or > @implementation, so it must be pure C. But isn't it self-referential? Is > it a C idiom? Any pointers would be appreciated.
Heh. "Pointers." Quite relevant. Yes, this is pure C code. And yes, it is self-referential. It declares a static pointer-to-void called AFVSPlayerItemStatusContext. It also initializes that variable to the address-of the variable AVSPlayerItemStatusContext. How can this work? Well, static variables are initialized at runtime. Since the address of the variable AVSPlayerItemStatusContext is known at runtime, this works swimmingly. The loader loads your executable at some offset, then initializes all the static variables in your executable. It can easily figure out the address of the static variable it's initializing—after all, it had to find it to initialize it! What might be tripping you up is the requirement that C static variables be initialized with constants. As far as the C definition is concerned, by the time the compiler has gotten to the initialization (the `= &AVSPlayerItemStatusContext` bit), the static variable AVSPlayerItemStatusContext has already been declared, so it knows that at runtime all the important bits will fall into place. --Kyle Sluder _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
