C++ Logo


Advanced search

Re: pointer to volatile, but no volatile object

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 06 Jun 2023 12:40:10 -0700
On Tuesday, 6 June 2023 02:40:52 PDT David Brown via Std-Discussion wrote:
> After all, volatile accesses to local non-volatile variables can
> sometimes be useful in the context of debugging - you can use such
> accesses to ensure that a variable is actually updated (at least from
> the viewpoint of the current thread) at a particular point in the code,
> where you might have placed a breakpoint.

In my experience, volatile gets (ab)used not for debugging, but to suppress
some compiler optimisation that the developer didn't want that does have some
visible side-effect. For example, I've seen it used to force accepting a signed
integer overflow, because the value had to be re-read from memory. In fact,
I've seen that in a security training that was given to developers about how
to remove undefined behaviour from their code... which didn't inspire me with

We also (ab)use it in our application at work to force specific codegen from
the compiler. Our application does some things no normal application does,
like recalculate the same thing over and over again with the same inputs to
see if it becomes different.

Needless to say, all those uses are very fragile and will likely break soon.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-06-06 19:40:13