C/C++ Function at memory address 1?(c++)

  • Thread starter Thread starter muppet
  • Start date Start date
  • Tags Tags
    Function Memory
AI Thread Summary
The discussion revolves around the use of function pointers in C++ with the GNU Scientific Library. The user encounters an issue where printing the addresses of function pointers results in the integer "1" instead of the expected hexadecimal values. The explanation clarifies that C++ does not provide a standard output method for printing function pointers directly. Instead, function pointers can only be converted to boolean values, leading to the unexpected output. It highlights that while pointers to data can be printed as hexadecimal addresses, function pointers are treated differently due to type strictness in C++. The suggestion is made to cast function pointers to void* for proper output, although this is technically undefined behavior. The discussion emphasizes the differences in pointer handling on various architectures, particularly between Von Neumann and Harvard architectures.
muppet
Messages
602
Reaction score
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?
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top