Volatile Example: The Value of the Keyword ‘Volatile’ in C

The keyword “volatile” informs the compiler that the value of a variable can be changed from the outside. This can occur under the control of the operating system, hardware, or another stream. Because the value is changeable, the compiler loads it from memory each time.

A volatile integer variable can be declared as:

int volatile х;
volatile int х;

To declare a pointer to this variable, do the following:

volatile int *х;
int volatile *х;

A volatile pointer on a nonvolatile data is used rarely, but:

int *volatile х;

If you want to declare a volatile pointer to a volatile memory area, you should do the following:

int volatile *volatile х;

The volatile variables are not optimized and it can be useful. Imagine the following function:

int opt = 1;
void Fn(void) {
    start:
        if (opt == 1)
            goto start;
        else
            break;
}

At first glance, the program will loop. The compiler can optimize it as follows:

void Fn(void) {
    start:
    int opt = 1;
    if (true)
        goto start;
)

Now the cycle will definitely be endless. However, the external operation allows writing 0 to the variable opt and break the cycle.

To prevent such optimization, you can use the keyword “volatile”, for example, to announce that some external element of the system changes the variable:

volatile int opt = 1;
void Fn(void) {
    start:
    if (opt == 1)
        goto start;
    else
        break;
}

Volatile variables are used as global variables in multithreaded programs. Any thread can change common variables. We do not want to optimize these variables.

Thanks for your attention!

References

McDowell, Gayle Laakmann. Cracking the Coding Interview: 150 Programming Questions and Solutions.

Our volatile example for C++ can help you to deal with your own assignment. However, you can’t hand in this sample as your own work. If you don’t want to handle your homework or don’t have time for it, you can get help from AssignmentShark.com. We do everything possible to satisfy our customers’ needs. Our experts work fast, so you can get a completed assignment even before the deadline.

If you want to get our help, all you need to do is to place an order with your requirements and set the deadline. We can solve any problem in your homework. Consider our service if you want to get help from a professional expert who has a degree in your sphere of study and experience in handling different types of assignments.

Using our service is a great benefit for all students. On our site, you have the opportunity to contact an expert directly via chat. We have reasonable prices, so any student can afford using our service. With our help, you can get the highest grades. We are the best option for all students with homework problems. We are a reliable service that will handle your assignments much quicker than you or anyone else.

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.