Function at memory address 1?(c++)

  • Context: C/C++ 
  • Thread starter Thread starter muppet
  • Start date Start date
  • Tags Tags
    Function Memory
Click For Summary
SUMMARY

The discussion centers on the behavior of function pointers in C++ when using the GNU Scientific Library. The user encountered unexpected output, receiving the integer "1" instead of the expected hexadecimal addresses for function pointers. The explanation provided clarifies that C++ does not have a standard method for printing function pointers directly, and that function pointers can only be converted to boolean values. It is recommended to cast function pointers to void* for proper address representation, although this practice is technically undefined behavior.

PREREQUISITES
  • Understanding of C++ function pointers
  • Familiarity with the GNU Scientific Library (GSL)
  • Knowledge of memory architecture concepts (Von Neumann vs. Harvard)
  • Basic experience with C++ output streams
NEXT STEPS
  • Research C++ function pointer casting techniques
  • Learn about the GNU Scientific Library functions and their applications
  • Explore memory architecture differences in programming
  • Study C++ output stream manipulation for custom data types
USEFUL FOR

C++ developers, software engineers working with scientific computing, and anyone interested in understanding function pointers and memory management in C++.

muppet
Messages
602
Reaction score
0
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::count<< y <<"\n";
std::count<< z <<"\n";
std::count<< fpt << "\n";
std::count<< fpt2 <<"\n";
std::count<< &gsl_sf_bessel_J0 << "\n";
std::count<< &gsl_sf_bessel_J1 << "\n";
std::count<< &x <<"\n";
std::count<< t <<"\n";
std::count<< u << "\n";
return 0;
}

And here's the output:

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

Thanks.
 
Technology news on Phys.org
muppet said:
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*.
 
I went on holiday the day after I wrote this; so a belated thank you for your informative reply.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
20
Views
2K
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
3
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K