Following program give me runtime error(Segmentation Fault (SIGSEGV)).
#include <iostream>
using namespace std;
int& bar()
{
int n = 10;
return n;
}
int main() {
int& i = bar();
cout<<i<<endl;
return 0;
}
My teacher told me it is a undefined behavior because dangling reference. Is he right? If yes then how to avoid it?