title: Is It Safe to Cast Away volatile?
body: <p>Most of the time, I am doing this way.</p>
  
  <pre><code>class a {
  public:
      ~ a() {
          i = 100;          // OK
          delete (int *)j;  // Compiler happy. But, is it safe?
                            // The following code will lead compilation error : delete j;
      }
  
  private:
      volatile int i;
      volatile int *j;
  };
  
  int main() {
      a aa;
  
  }
  </code></pre>
  
  <p>However, I saw an article here:</p>
  
  <p><a href="https://www.securecoding.cert.org/confluence/display/seccode/EXP32-C.+Do+not+access+a+volatile+object+through+a+non-volatile+reference" rel="nofollow noreferrer">https://www.securecoding.cert.org/confluence/display/seccode/EXP32-C.+Do+not+access+a+volatile+object+through+a+non-volatile+reference</a></p>
  
  <blockquote>
    <p>Casting away volatile allows access to
    an object through a non-volatile
    reference. This can result in
    undefined and perhaps unintended
    program behavior.</p>
  </blockquote>
  
  <p>So, what will be the workaround for my above code example?</p>
  
  <p>Here is the error message I get if I use </p>
  
  <blockquote>
    <p>delete j</p>
  </blockquote>
  
  <p>Note that, this is output from VC6 (Don't ask why I am using VC6!)</p>
  
  <blockquote>
    <p>c:\projects\a\a.cpp(5) : error C2664:
    'delete' : cannot convert parameter 1
    from 'volatile int *' to 'void *'
    Conversion loses qualifiers</p>
  </blockquote>