Function at memory address 1?(c++)

In summary, the conversation discusses the use of function pointers in the GNU scientific library and how they can be used to evaluate different functions. The output of the code includes a pointer to a double, which is converted to a void* for printing, and the explanation for this behavior is due to differences in pointer types on different computer architectures.
  • #1
muppet
608
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
  • #2
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*.
 
  • #3
I went on holiday the day after I wrote this; so a belated thank you for your informative reply.
 

1. What is a memory address in C++?

A memory address in C++ is a unique identifier for a location in the computer's memory. It is represented by a hexadecimal number and is used to access and store data.

2. How do I find the memory address of a variable in C++?

You can use the ampersand (&) operator before the variable's name to obtain its memory address. For example, int x = 5; cout << &x; will print the memory address of variable x.

3. What does it mean to "function at memory address 1" in C++?

When a function is said to be at a specific memory address, it means that the function's code is stored at that location in the computer's memory. This is important for debugging and optimizing code.

4. How can I call a function at a specific memory address in C++?

In general, it is not recommended to call a function at a specific memory address as it can cause unexpected behavior. However, if needed, you can use function pointers to call a function at a specific memory address.

5. Is it possible to change the memory address of a function in C++?

No, it is not possible to change the memory address of a function in C++. The memory address is determined during compilation and cannot be altered at run time.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
718
  • Programming and Computer Science
Replies
6
Views
884
  • Programming and Computer Science
Replies
11
Views
986
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top