Wipro Technologies Interview Question
Software AnalystsCountry: India
Interview Type: In-Person
Global constant created on read only data segment of memory so you can not modify its value ... variable inside main created on stack to it can be modified ...
A.) The scope of 'temp' is global and is of type constant so we cannot change the value of a function inside the function fun()
B.) The way the temp argument passed to fun is by using "const_cast" which removes constness of the variable 'temp'.
@Durga, In the following program, why the assignment in fun() "temp=2000" only casues crash and not the assignment in main "b = 30"?
#include <iostream>
#include <stdio.h>
using namespace std;
class A{
public:
void fun(int& temp){
cout<<"1am inside fun()"<<endl;
temp = 2000;
cout<<"2am inside fun()"<<endl;
printf("in fun() temp[%p] temp val[%d]\n",&temp, temp);
}
};
const int temp=100;
int main()
{
A a;
printf("in main() temp[%p] temp val[%d]\n",&temp, temp);
int b = const_cast<int&>(temp);
b = 30;
cout << "b:" << b << endl;
a.fun(const_cast<int&>(temp));
cout<<"temp:"<<temp<<endl;
}
Agreed with Sina regarding the reason for the crash & why moving temp decl inside main 'fixes' it. But no one's answered the other part of the question -- once change is made, why does observed value change for fun() but not for main() ?
That's because optimizing compilers will typically cache data in processor registers when they have no reason to think the data will change (if a register is available). Declaring temp 'const' explicitly tells the compiler it's OK to do that. So main copies the initial value of temp to a register, and never re-loads it from memory; fun() changes the value in memory, but that new value is never read by main().
This is a compiler specific problem, in most compilers const_casting to non const variable and then changing the value is 'undefined', so this is a terrible question to ask in an interview, however in most compilers what happens is as follows:
A) The public scope variable which has a explicit value assigned to it can be determined by compiler at compile time and is replaced at "compile time" by literals, i.e. the
"100", or more specifically, depending on compiler, to an address in code segment instead of data segment, so there is no actual memory allocated for temp. Changing the value of temp to 2000 in "fun" is an attempt to change an undefined memory or a memory in code segment which causes the crash. This is not for all cases, if we change the code to something like this:
then there will be actual memory allocated to temp and the program does not crash, even though the const variable is still in global scope. but it all depends on the cleverness of the compiler.
- Sina January 20, 2013B) In the other hand, local consts inside functions are always allocated actual memory in most compilers, this is probably because of efficiency in accessing local variable in function's memory in stack in comparison to a value in code segment. So again, same happens as example above and we won't have any crash.