NVIDIA Agilent Technologies Interview Question for Software Engineer / Developers






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

Am not quite sure whether you are right.
i think volatile instructs the compiler not to optimse the storage of a variable declared as volatile (i.e. not to sorta cache it) since it is subject to change by another process.

- Joseph Patrao February 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

"A Java(TM) programming language keyword used in variable declarations that specifies that the variable is modified asynchronously by concurrently running threads."

"Subject to change. The volatile keyword informs the compiler that a variable could change value at any time (because it is mapped to a hardware register, or because it is shared with other, concurrent processes) and so should always be loaded before use."

- Jack February 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This got me curious so I read about volatile with respect to C. Sometimes the compiler may optimize code by placing a variable's value into a register. Volatile prevents this optimization.

- Jack February 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The value stored in the variable marked as volitile can change at any time.(Example: it is connected to some external circuit which causes new values to be stored at that location at random times). Therefor the compiler should NOT perform any read optimizations that would cache the value. Each time the value is to be read, the value should be read directly from memory/register.

- Paparazzi February 21, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I wasn't sure what volatile really was until recently.

From my understanding, it ensures that the program works with the most recent copy of a value. Specifically, if an interrupt service routine changes the value of a variable and function uses a register instead of the variable, the value will be lost to the function. Volatile ensures that the compiler doesn't perform this optimization.

- Jack March 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Also very useful in multi-threaded applications.

- Amod March 17, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wouldn't you use a critical section (semaphores, mutex, etc)for these types of variables if multiple threads are accessing it? Or volatile meaning something outside the process can modify the variable? Any examples?

- Ryan May 01, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Threaded programming solves only part of the problem. The biggest use of volatile is in low-level programming whereby you're trying to send a signal to an i/o device or the I/O device is asynchronously modifying the value of a memory location. in these situations you dont want to " act smart" and try to optimize accesses by storing copies in registers that could be stale.
Also, say you're trying to send a digital signal to a HW peripheral

int signal = 0;
//insert code to pause for some time
signal = 1;

compiler may optimize this simply to signal = 1 so you lose your intended functionality. Thus need volatile keyword

- threads May 15, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Uses of the ‘volatile’ Keyword

The volatile specifier disables various optimizations that a compiler might automatically apply and thus introduce bugs. I’ll give two examples:

1) Some hardware interfacing applications periodically read values from a hardware port and copy it to a local variable for further processing. A good example is a timer function that reads the current count from an external clock. If the variable is const, the compiler might skip subsequent reads to make the code more efficient and store the result in a CPU register. To force it to read from the port every time the code accesses it, declare the variable const volatile.

2) In multithreaded applications, thread A might change a shared variable behind thread B’s back. Because the compiler doesn’t see any code that changes the value in thread B, it could assume that B's value has remained unchanged and store it in a CPU register instead of reading it from the main memory. Alas, if thread A changes the variable’s value between two invocations of thread B, the latter will have have an incorrect value. To force the compiler not to store a variable in a register, declare it volatile.
Danny Kalev

- Volatile March 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://en.wikipedia.org/wiki/Volatile_variable

- Anonymous October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

volatile means that the variable can be changed from outside the owner process.
this is useful when developing drivers. people want to map certain hardware port to a memory space.

variables that are volatile can't be optimized by the compiler.

- Anonymous February 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Please correct me if I'm wrong...

volatile preserves the endianess of a value in memory.

- Jack February 04, 2006 | 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