What does—or did—"volatile void function( ... )" do?

EdwinW picture EdwinW · Jan 12, 2013 · Viewed 14.8k times · Source

I've seen How many usage does "volatile" keyword have in C++ function, from grammar perspective? about use of the volatile keyword on functions, but there was no clear explanation of what Case 1 from that question did. Only a statement by one of the respondents that it seemed pointless/useless.

Yet I cannot quite accept that statement, since the AES software implementations for GNUC have been used for literally years, and they have a number of functions like this:

INLINE volatile void functionname( /* ... */ ) {
    /* ... */
    asm( /* ... */ ) // embedded assembly statements
    /* ... */
}

There has to have been a reason for that usage. Can anyone:

A. tell me what the original reason was; and

B. how to achieve the desired effect now?

I'm using Ubuntu, and GCC 4.6.3.


Note: The closest I've come to an explanation is that prior to GCC 2.5, you could spoof the 'noreturn' attribute that was implemented in 2.5 via the following:

void fatal( /* ... */ ) { /* ... */ exit(1); }

typedef void voidfn ();

volatile voidfn fatal;

This would allow the compiler to recognize that 'fatal' was not going to return.

But that scenario doesn't appear to apply to the AES code. It's been a long time since I did anything in assembly, but I think I'd recognize a jump or something like that.

Answer

Chris Dodd picture Chris Dodd · Jan 12, 2013

According to the gcc documentation (until February 2015), volatile void as a function return value in C (but not in C++) is equivalent to __attribute__((noreturn)) on the function and tells the compiler that the function never returns.