Is It Safe to Cast Away volatile?

Posted by Yan Cheng CHEOK on Stack Overflow See other posts from Stack Overflow or by Yan Cheng CHEOK
Published on 2010-03-19T03:11:36Z Indexed on 2010/03/19 3:21 UTC
Read the original article Hit count: 213

Filed under:

Most of the time, I am doing this way.

class a {
public:
    ~ a() {
        i = 100;          // OK
        delete (int *)j;  // Compiler happy. But, is it safe?
                          // Error : delete j;
    }

private:
    volatile int i;
    volatile int *j;
};

int main() {
    a aa;

}

However, I saw an article here:

https://www.securecoding.cert.org/confluence/display/seccode/EXP32-C.+Do+not+access+a+volatile+object+through+a+non-volatile+reference

Casting away volatile allows access to an object through a non-volatile reference. This can result in undefined and perhaps unintended program behavior.

So, what will be the workaround for my above code example?

© Stack Overflow or respective owner

Related posts about c++