Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:57:55+00:00 2026-06-14T18:57:55+00:00

I’m going through a tutorial on audio for the iphone and it uses C/C++.

  • 0

I’m going through a tutorial on audio for the iphone and it uses C/C++. I’m not familiar with the use of THIS->. It seems to refer to a pointer to global variable. Here is the tutorial – iPhone Core Audio Part 3 – Audio Callback.

The statement I am trying to understand is the THIS-> part of the statement:

// Pass in a reference to the phase value, you have to keep track of this
// so that the sin resumes right where the last call left off
float phase = THIS->sinPhase;

The tutorial indicate that THIS-> is used to get a to access AudioController variables. It seems that sinPhase is global variable.

Please explain why “phase” reference is created instead of just referring directly to the global variable “sinPhase”. Keep in mind I am an objective C programming trying to understand this C/C++ code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:57:56+00:00Added an answer on June 14, 2026 at 6:57 pm

    In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon:

    static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
    {
        // Get a reference to the object that was passed with the callback
        // In this case, the AudioController passed itself so
        // that you can access its data.
        AudioController *THIS = (AudioController*)inRefCon;
    

    This is a fairly common pattern in C; in order to pass a callback in to some API, so that it can later call your code, you pass both a function pointer and a void pointer. The void pointer contains whatever data your function pointer will need to operate on. Within your callback, you will need to cast it back to a pointer to the actual type, so you can access the data within it. In this case, the author of the example is naming that cast pointer THIS, probably to make this look more object-oriented, even though this is just C and THIS has no special meaning.

    You ask why they assign it to a local variable rather than just using THIS->sinPhase everywhere. There’s no reason you couldn’t use THIS->sinPhase everywhere; they likely just assigned it to a local variable phase to save on typing. There’s a small chance that the optimizer could do a better job on a local variable than on one passed in via a pointer, because it can make more assumptions about the local variable (in particular, it can assume that no one else is updating it at the same time). So the loop might run slightly faster using a local variable, though I wouldn’t be certain without testing; the most likely reason is just to save typing and make the code more readable.

    Here’s a simplified example of how a callback API like this works; hopefully this should make it easier to understand how a callback API works, without trying to understand the rest of what’s going on in Core Audio at the same time. Let’s say I want to write a function that will apply a callback to an integer 10 times. I might write:

    int do_ten_times(int input, int (*callback)(int)) {
        int value = input;
        for (int i = 0; i < 10; ++i) {
            value = callback(value);
        }
        return value;
    }
    

    Now I could call this with different functions, like the following add_one() or times_two():

    int add_one(int x) {
        return x + 1;
    }
    
    int times_two(int x) {
        return x * 2;
    }
    
    result = do_ten_times(1, add_one);
    result = do_ten_times(1, times_two);
    

    But say I want to be able to add or multiply by different numbers; I could try writing one function for each number that you wanted to add or multiply by, but then you would run into a problem if the number wasn’t fixed in the code, but was based on input. You can’t write one function for each possible number; you are going to need to pass a value in. So let’s add a value to our callbacks, and have do_ten_times() pass that value in:

    int do_ten_times(int input, int (*callback)(int, int), int data) {
        int value = input;
        for (int i = 0; i < 10; ++i) {
            value = callback(value, data);
        }
        return value;
    }
    
    int add(int x, int increment) {
        return x + increment;
    }
    
    int times(int x, int multiplier) {
        return x * multiplier;
    }
    
    result = do_ten_times(1, add, 3);
    result = do_ten_times(1, times, 4);
    

    But what if someone wants to write a function that varies by something other than an integer? For instance, what if you want to write a function that will add different numbers depending on whether the input is negative or positive? Now we need to pass two values in. Again, we could extend our interface to pass in two values; but we will eventually need to pass in more values, values of different types, and the like. We notice that do_ten_times really doesn’t care about the type of the value we’re passing in; it just needs to pass it to the callback, and the callback can interpret it however it likes. We can achieve this with a void pointer; the callback then casts that void pointer to the appropriate type to get the value out:

    int do_ten_times(int input, int (*callback)(int, void *), void *data) {
        int value = input;
        for (int i = 0; i < 10; ++i) {
            value = callback(value, data);
        }
        return value;
    }
    
    int add(int x, void *data) {
        int increment = *(int *)data;
        return x + increment;
    }
    
    int times(int x, void *data) {
        int multiplier = *(int *)data;
        return x * multiplier;
    }
    
    struct pos_neg {
        int pos;
        int neg;
    };
    int add_pos_neg(int x, void *data) {
        struct pos_neg *increments = (struct pos_neg *)data;
        if (x >= 0)
            return x + increments->pos;
        else
            return x + increments->neg;
    }
    
    int i = 3;
    result = do_ten_times(1, add, &i);
    int m = 4;
    result = do_ten_times(1, times, &m);
    struct pos_neg pn = { 2, -2 };
    result = do_ten_times(-1, add_pos_neg, &pn);
    

    These are all, of course, toy examples. In the Core Audio case, the callback is used to generate a buffer of audio data; it is called every time the audio system needs to generate more data in order to keep playing smoothly. The information passed via the void *inRefCon is used to track how exactly where in the sine wave you have gotten to in the current buffer, so the next buffer can pick up where the last one left off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.