title: C: Volatile Arrays in C
body: <p>The volatile keyword is used in C to prevent the compiler performing certain optimizations, amongst other subtle changes, on a variable.</p>
  
  <p>For example;</p>
  
  <pre><code>volatile int my_int = 0;
  </code></pre>
  
  <p>creates an integer. In some situations it may prevent the following optimization:</p>
  
  <pre><code>while(my_int == 0); // Loop until my_int != 0
  </code></pre>
  
  <p>Optimize to:</p>
  
  <pre><code>while(1); // Loop infinity.
  </code></pre>
  
  <p>This is useful for situations including those frequently encountered in embedded systems, such as a situation where modification to a variable may be made by an interrupt function call. There are many other examples of where this technique is useful. <code>my_int</code> may be a flag which is modified by such a function. (This is just a toy model.)</p>
  
  <p>However, consider the case where the data modified by the function is an array. The data may be pointed to by a pointer.</p>
  
  <pre><code>unsigned char* my_data = new unsigned char[256];
  </code></pre>
  
  <p>In this case, considering that my_data is a global variable in this specific situation of this question[1], <em>is the <code>volatile</code> keyword redundant, or is it still required?</em></p>
  
  <p>[1] It may not matter.</p>
  
  <p>If the answer is that the volatile keyword is required, what it the correct syntax for use?</p>
  
  <p>For example, <code>volatile unsigned char* my_data</code>, I <em>assume</em> declares that the pointer itself is volatile, and not the data it points to.</p>
  
  <p>Finally, is there a difference between the use in C and C++?</p>