Gluster Interview Question
Software Engineer / DevelopersIn C, global variables can only be initialized with constants, numeric values, NULL or fixed strings. The expression 1/0 does not evaluate to a numeric value and hence it fails.Also, in the first example, int j = *i is not a valid constant expression. Hence the error 'intializer element is not constant'
#include <iostream>
using namespace std;
class A{
int i,j;
public:
A():i(1),j(0)
{
i = i/j; // here it crashes before it reaches to Main() func because of divided-by-zero
cout<< "A class's constructor \n";
}
};
A a;// Global object , it invokes A's constructor
int main()
{
cout<< "hello after main"<<endl;
return 0;
}
constructor for Global object of classes are invoked before main() is invoked. So you can do anything bad there resulting in a crash.
- Mike February 13, 2011