0

I have following code snippet, what does &main and &user depict here? Each time I run why does it give different values? In what scenarios passing &function_name is useful?

int user()
{
      return 0;  
}

int main()
{
    int a, b = 0;
    a = ((int)&main);
    b = ((int)&user);
    cout << a <<" " << b;
    return 0;
}
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31
Aman
  • 339
  • 3
  • 12
  • Read about function pointers in your favourite C++ book. – molbdnilo Nov 06 '17 at 11:45
  • The use of function pointers is the only way to solve certain problems. It's useful in those scenarios. – DeiDei Nov 06 '17 at 11:45
  • 1
    In the examples above it depicts unspecified behavior. – Sergey Kalinichenko Nov 06 '17 at 11:47
  • I remember referring to `main` (or only calling it?) is UB. Whatever the source of this code, it's not good. Replace it with [one of these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – nwp Nov 06 '17 at 11:50
  • The C++ standards forbid any usage of the `main()` function in a program - including taking its address (as occurs with `&main` within the `main()` function). The result of doing so gives undefined behaviour. – Peter Nov 06 '17 at 12:28

2 Answers2

0

It depcits the address of your functions. The reason why its different each time you run it, is because your program probably does not use the same memory space each time.

&main gives you the address of your main function, quite like &var gives you the address of a variable called var. The cast to int is probably problematic. Pointers on a 64 bit system are 64 bit wide, an int may not be that long.

Hafnernuss
  • 2,659
  • 2
  • 29
  • 41
0

sbi explained it nicely here:

Most examples boil down to callbacks: You call a function f() passing the address of another function g(), and f() calls g() for some specific task. If you pass f() the address of h() instead, then f() will call back h() instead.

Basically, this is a way to parametrize a function: Some part of its behavior is not hard-coded into f(), but into the callback function. Callers can make f() behave differently by passing different callback functions. A classic is qsort() from the C standard library that takes its sorting criterion as a pointer to a comparison function.

In C++, this is often done using function objects (also called functors). These are objects that overload the function call operator, so you can call them as if they were a function. Example:

class functor {
  public:
     void operator()(int i) {std::cout << "the answer is: " << i << '\n';}
};

functor f;
f(42);

The idea behind this is that, unlike a function pointer, a function object can carry not only an algorithm, but also data:

class functor {
  public:
     functor(const std::string& prompt) : prompt_(prompt) {}
     void operator()(int i) {std::cout << prompt_ << i << '\n';}
  private:
     std::string prompt_;
};

functor f("the answer is: ");
f(42);

Another advantage is that it is sometimes easier to inline calls to function objects than calls through function pointers. This is a reason why sorting in C++ is sometimes faster than sorting in C.

Qrchack
  • 899
  • 1
  • 10
  • 20