Adobe Interview Question
Software Engineer / DevelopersQuestion is very clear. Asking for possible (and infact most commonly opted wrong progrmming style) bug that can result into crash.
Solution.
int * sum(int a,int b){
// Initialize a local pointer.
int *ptr = &(a +b);
return ptr;
}
main(){
cout<<"sum = "<<*(sum(1,2))<<endl;
}
//here in main when trying to access pointer that is local to sum function and is already died as soon as control came out from sum, it may cause crash. (plateform dependent)
I have a serious doubt.How can u use &(a+b) to initialize pointer p. You should use an extra variable c=a+b and then use int *p=&c;
But the solution seems correct otherwise because the local pointer has already run our of scope and the pointer returned is definite to make the programme crash :)
cout<<"sum = "<<*(sum(1,2))<<endl;
why this line will not print 3 as output. but if we assign it to other pointer variable and then print its value it will print the value as 3. Could u explain ?
Although the program will crash at some time... but still could u explain the above reason. It prints 0...
please explain a bit more, your question is not clear
- Anonymous May 04, 2009