C

Topic: Memory Allocation

when should the volatile modifier be used?

The volatile modifier is a directive to the compiler's optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer's hardware as if it were part of the computer's memory), and the second involves shared memory (memory used by two or more programs running simultaneously).

Most computers have a set of registers that can be accessed faster than the computer's main memory. A good compiler will perform a kind of optimization called 'redundant load and store removal.' The compiler looks for places in the code where it can either remove an instruction to load data from memory because the value is already in a register, or remove an instruction to store data to memory because the value can stay in a register until it is changed again anyway. If a variable is a pointer to something other than normal memory, such as memory-mapped ports on a peripheral, redundant load and store optimizations might be detrimental. For instance, here's a piece of code that might be used to time some operation: 

time_t time_addition(volatile const struct timer *t, int a){
int n;int x; time_t then; x = 0;
then = t->value;for (n = 0; n < 1000; n++){x = x + a;}return t->value - then;}

In this code, the variable t->value is actually a hardware counter that is being incremented as time passes. The function adds the value of a to x 1000 times, and it returns the amount the timer was incremented by while the 1000 additions were being performed. Without the volatile modifier, a clever optimizer might assume that the value of t does not change during the execution of the function, because there is no statement that explicitly changes it. In that case, there's no need to read it from memory a second time and subtract it, because the answer will always be 0. The compiler might therefore ?optimize? the function by making it always return 0.If a variable points to data in shared memory, you also don't want the compiler to perform redundant load and store optimizations. Shared memory is normally used to enable two programs to communicate with each other by having one program store data in the shared portion of memory and the other program read the same portion of memory. If the compiler optimizes away a load or store of shared memory, communication between the two programs will be affected.

Browse random answers:

what is Memory Allocation?
What is the heap? 
Difference between calloc and malloc ? 
Why does malloc(0) return valid memory address ? What's the use ?
How can you determine the size of an allocated portion of memory?
What are advantages and disadvantages of external storage class?
What is the purpose of realloc( )?
Difference between calloc() and malloc()?
What is the difference between new/delete and malloc/free?
What?s wrong with this code?Char*p*p=malloc(10);
Is it better to use malloc() or calloc()?
Why do we need to test weather it is memory leak or not? How are we going to know that?
what is the difference between the functions memmove() and memcpy()?
How to write calloc() in terms of malloc()? ie malloc() should initialise the memory to zero after allocating it ?
when should the volatile modifier be used?
How can you determine the size of an allocated portion of memory ?
when memory will be created after defining in c and c++;
What is static memory allocation and dynamic memory allocation?
What?s wrong with this code?Char*p*p=malloc(10);
How can you determine the size of an allocated portion of memory ?
Difference between malloc and calloc?
What is the difference between calloc() and malloc() ?
What are the differences between formal arguments and actual arguments?
 What is static memory allocation and dynamic memory allocation? 
What is difference between template and macro?
How do I compare character data stored at two different memory locations? 
How do I compare character data stored at two different memory locations?
What are memory models?
How to get the memory size ?
How to use realloc() to dynamically increase size of an already allocated array?
How do I write code that reads data at memory location specified by segment and offset?
How do I compare character data stored at two different memory locations?