0

What do these values represent or come from in the following code?

4196598
1

Why does the following code output this above:

#include <iostream>

using namespace std;

void someFunction() {
    cout << "someFunction" << endl;
}

int main()
{
 
    cout << long(someFunction) << endl;
    cout << someFunction << endl;

    return 0;
}
Chase W
  • 3
  • 1
  • 1
    It's actually the location of the function (ie. the location for the instructions to be jumped to), casting it to long, gave us the number, which will likely change during multiple invocations. – AdityaG15 May 29 '21 at 19:36
  • As for the "cout << someFunction", can't be sure, though it seems to output '1', for other signatures too, also &someFunction, and *someFunction is printed as '1' by cout. – AdityaG15 May 29 '21 at 19:44

1 Answers1

0

It's actually the location of the function (ie. the location for the instructions to be jumped to), casting it to long, gave us the number, which will likely change during multiple invocations.

Debugging the same code in gdb, you can get the location in hex, like this -

gdb

When you convert the hex number to a decimal number, you will get 93824992235913... and that's what the cast gives us.

Regarding function and what their function name denotes, there's a good amount of knowledge at Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?.

Quoting one comment from there,

A function name is really just a label on a memory address. The environment doesn't actually see separate functions and pointers to them - it just sees an instruction branch to an address. This is different to objects for which an environment will use different addressing modes (direct or indirect)

AdityaG15
  • 290
  • 3
  • 12