CSR Interview Question


Country: United States




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

First of all CSR asks always stupid questions and this question is not any different.I hope they stop asking stupid questions which they themselves are not clear about.

volatile means the value of that variable is always read from its memory location on access and written to its memory location on mutation.So any optimization(by the compiler) that doesn't affect this can still be used.
Compiler uses all the tricks to make your code faster such as 'tail call'.
Most of the time the compiler would read the memory to get the value but in below code it may not do that as it can find out(probably by static analysis) that this value is not going to change so it will optimize it.

foo()
{      
      cpsr_reg = 30; /* this is cpu register */
      printf("%d\n", cpsr_reg);
}

Compiler will optimise it as below:

foo()
{      
      cpsr_reg = 30; /* this is cpu register */
      /* but cpsr_reg can change between the above and below instruction */
      printf("%d\n", 30);
}

So by adding volatile you are just instructing the compiler to not do this and compiler.
How does it do that would be specific to compiler.

- aka January 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the volatile is being implemented by reserving a pool of certain memory for the usage even if the memory goes over the system capacity so that it can be reused.

- hprem991 January 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By declaring a variable as volatile, the O.S forces the process to read the value from Main M/Y, not from the cache. So, the variable declared as volatile, will have the latest value.

- Prashanth August 29, 2013 | Flag Reply


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