Thread: Where Function pointers are stored ?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Where Function pointers are stored ?

    Hi

    Where function are stored in memory?


    In the following example , where function pointer fp stored ?
    Code:
    #include <math.h> #include <stdio.h> // Function taking a function pointer as an argument double compute_sum(double (*funcp)(double), double lo, double hi) {
    double sum = 0.0; // Add values returned by the pointed-to function '*funcp' for (int i = 0; i <= 100; i++)
    {
    double x, y; // Use the function pointer 'funcp' to invoke the function x = i/100.0 * (hi - lo) + lo; y = (*funcp)(x); sum += y;
    }
    return sum;
    } int main(void) {
    double (*fp)(double); // Function pointer double sum; // Use 'sin()' as the pointed-to function fp = &sin; sum = compute_sum(fp, 0.0, 1.0); printf("sum(sin): %f\n", sum); // Use 'cos()' as the pointed-to function sum = compute_sum(&cos, 0.0, 1.0); printf("sum(cos): %f\n", sum); return 0;
    }


  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It depends on where you declare it. In the code above, it is stored on the stack, just like sum or any other local variables, like if you had a char * or int * variable. You could dynamically allocate space for a function pointer, and have it be stored on the heap, or declare it as a global or static local, and have it be stored with the other global and static local variables, but that is less common.

    Remember, a function pointer is just a pointer, so all it contains is the address. It doesn't store the function itself (i.e. no code or variables).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-27-2012, 09:10 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Calling a function from a stored object
    By Walker in forum C++ Programming
    Replies: 6
    Last Post: 01-26-2010, 07:08 AM
  4. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

Tags for this Thread