Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Function at memory address 1?(c++)

  1. Aug 23, 2012 #1
    Hi all,

    I'm trying to play about with the GNU scientific library, which means learning how to use function pointers. When I run the code below, I can use function pointers to successfully evaluate two different functions. But I decided to try and look at the function addresses, and instead of the hexidecimal numbers I expected I got the integer "1" for both values. Can someone please explain to me what's going on?

    #include <iostream>
    #include <gsl/gsl_sf_bessel.h>
    #include <gsl/gsl_math.h>
    #include <gsl/gsl_integration.h>

    int main()
    double x = 5.0;
    double y = gsl_sf_bessel_J0(x); //me using functions from the GNU scientific library
    double z = gsl_sf_bessel_J1(x); //ditto
    double t, u;

    double (*fpt)(double) = &gsl_sf_bessel_J0;
    double (*fpt2)(double) = &gsl_sf_bessel_J1;
    t= (*fpt)(x);
    u= (*fpt2)(x);

    std::cout<< y <<"\n";
    std::cout<< z <<"\n";
    std::cout<< fpt << "\n";
    std::cout<< fpt2 <<"\n";
    std::cout<< &gsl_sf_bessel_J0 << "\n";
    std::cout<< &gsl_sf_bessel_J1 << "\n";
    std::cout<< &x <<"\n";
    std::cout<< t <<"\n";
    std::cout<< u << "\n";
    return 0;

    And here's the output:


  2. jcsd
  3. Aug 23, 2012 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The reason you're getting that 0xbfb50420 printed for that pointer to a double is because
    1. While there is no standard output stream method for printing a pointer to a double, there is a method for printing void pointers (void*): It prints the address in hex.
    2. Any pointer is automatically convertible to a void*.
    There similarly is no method for printing a double (*function_that_takes_and_return_a_double)(double). When you pass a function pointer as an argument, the underlying infrastructure has to look for a suitable conversion. Surprisingly, the only conversion that's available for function pointers is to convert them to bool. Your function pointers aren't null, hence the ones for output.

    So why doesn't the language use that handy conversion to void* that was used for a pointer to a double? The answer is that function pointers aren't pointers. C++ is very strict with types and is designed to work on a variety of computers. On Harvard architecture machines, pointers to program memory can be very different beasts than pointers to data memory. Those two types of pointers might not even be the same size.

    You are almost certainly working on a Von Neumann architecture machine, where pointers are pointers, including function pointers. Try casting those function pointers to void*. Strictly speaking, this is undefined behavior. Practically speaking, the behavior is defined (and works) on most computers. It has to. The Windows and Unix dynamic library facilities wouldn't work without the ability to cast function pointers to/from void*.
  4. Sep 4, 2012 #3
    I went on holiday the day after I wrote this; so a belated thank you for your informative reply.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook