• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: static void declaration in apple example code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: static void declaration in apple example code


  • Subject: Re: static void declaration in apple example code
  • From: Kyle Sluder <email@hidden>
  • Date: Fri, 12 Oct 2012 01:07:19 -0700

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@hidden)

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:

This email sent to email@hidden


References: 
 >static void declaration in apple example code (From: Boris Dobroslav <email@hidden>)

  • Prev by Date: Re: static void declaration in apple example code
  • Next by Date: Re: Self contained command line tool
  • Previous by thread: Re: static void declaration in apple example code
  • Next by thread: Re: static void declaration in apple example code
  • Index(es):
    • Date
    • Thread