Function at memory address 1?(c++)

  • C/++/#
  • Thread starter muppet
  • Start date
  • #1
604
1

Main Question or Discussion Point

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?
Code:

#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:

-0.177597
-0.327579
1
1
1
1
0xbfb50420
-0.177597
-0.327579

Thanks.
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
1
0xbfb50420
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*.
 
  • #3
604
1
I went on holiday the day after I wrote this; so a belated thank you for your informative reply.
 

Related Threads on Function at memory address 1?(c++)

  • Last Post
Replies
7
Views
2K
Replies
15
Views
501
  • Last Post
Replies
15
Views
5K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
17
Views
4K
Replies
16
Views
927
  • Last Post
Replies
16
Views
3K
Replies
3
Views
1K
Replies
8
Views
3K
  • Last Post
Replies
2
Views
2K
Top