0

Why the output is 30 here? And how fun()=30 is valid? What is meant by assigning function a value? And why removing static keyword throws segmentation fault?

#include<iostream> 
using namespace std; 

int &fun() 
{ 
    static int x = 10; 
    return x; 
} 
int main() 
{ 
    fun() = 30; 
    cout << fun(); 
    return 0; 
} 
Ravi Maurya
  • 157
  • 7
  • 6
    It's time to pick up a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or two, specifically the part where they demonstrate "references". – Blaze Sep 05 '19 at 09:18
  • 4
    `fun()` returns a reference to the static variable `x` - and you are assigning to that. Not to a "function". – P.P Sep 05 '19 at 09:18
  • 1
    writing `int& fun()` is a tiny step towards avoid such misunderstanding – 463035818_is_not_an_ai Sep 05 '19 at 09:30

2 Answers2

4

Let's walk through your program step by step:

  1. inside main() fun() is called.
  2. fun() has a static variable x (static means, that x is stored in a special part of memory and not on the stack, so its value is preserved between function calls) (It's like having a global variable that is only visible from inside fun())
  3. fun() returns a reference to x (references are almost like pointers)
  4. You write to the returned reference of x, so x actually gets changed! (You don't write to the function)
  5. now x is 30 and fun() returns 30 on the next call.

I hope this answers your first three questions.

Why you get a segmentation fault without the static keyword:

In this case x does exist on the stack. So whenever you call fun() some memory gets allocated on the stack to hold x. When fun() returns, this memory will be deallocated.

Now, the reference returned by fun(), will reference a piece memory, that is not allocated by your program anymore. In other words, the returned reference will reference a memory address, that does "not belong to your program", thus you are not allowed to write to it. And so you get a segmentation fault.

Can we assign values to function:

To answer the actual title of your question: Yes, we can, using function pointers:

int foo(int x) {
    return x + 1;
}

int bar(int x) {
    return x * 2;
}

void main() {
    int(*f)(int) = foo;
    // int   the function pointed to returns an int
    // (*f)  f is the name of the function pointer
    // (int) the function pointed to takes on int as paramter
    // = foo; f now points to the function foo()

    (*f)(3); // the same as foo(3), returns 4
    f = bar; // now f points to bar()
    (*f)(3); // the same as bar(3), returns 6
}
Community
  • 1
  • 1
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 2
    `.data` or `.bss` (where `static`s are stored) segemnts are not the same as heap. – Fureeish Sep 05 '19 at 09:45
  • No, you can't assign values to functions. You can assign values to function pointers, but not to functions in C++. Try assigning anything to `foo` in your example, for instance. – Aykhan Hagverdili Sep 05 '19 at 17:43
0

fun() returns a reference to static variable x, to which you assign the value 30. You don't assign anything to the function itself. In fact, it is not possible to assign anything to a function in C++.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93