Interview Question


Country: India




Comment hidden because of low score. Click to expand.
2
of 2 vote

You can't cast away constness like that. In other words, your use of const_cast is illegal.

Let me begin by saying this: you're doing something that's not allowed, and it's undefined behavior. We can speculate about the precise technical cause of what you're observing, but at the end of the day, the behavior is compiler-dependent and undefined.

That said, I can try to give a probable explanation of why you're seeing the results you're seeing. The statement cout <<a<<" Address a "<<&a<<endl; is probably being changed to cout <<0<<" Address a "<<&a<<endl; at *compile time*. This is because the compiler automatically does const substitution. After all, the value can't change, so that optimization is safe, right? This is also why you can't arbitrarily cast away constness.

You cannot cast away constness on something that is actually const. This is because, among other reasons, you don't know what performance optimizations may have been performed on occurrences of that variable. The only legitimate use of the const cast is to cast a pointer that is declared a pointer to const *but that you KNOW points to a non-const object or primitive* to a pointer to non-const so that you may then use the non-constness of the object/primitive. Situations where the need to have something like this arises are rare.

- eugene.yarovoi July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for your value added comment but i do not get why the "*ptr = *ptr+10;" get changed and its still hold the same address as 'a' variable.

- Ajay Yadav Aricent July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Because there's no "a" variable. After the code compiles, it looks like

cout << 0 <<" Address a "<< some_number <<endl;
cout <<*ptr<<" Address in ptr "<<ptr<<endl;

The references to "a" might be being completely removed at compile time. So then it could well be that ptr = some_number (that is &a, determined at compile time), and it could well be that *ptr = 10, but the 0's baked right into the code as a literal. There is no "a". All expressions using "a" have been rewritten with the literal constant.

- eugene.yarovoi July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Eugene Yarovoi

- Ajay Yadav Aricent July 10, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More