Is "volatile" sufficient to prevent the C ++ compiler from optimizing recording without sound?

I would like to write a function that runs a copy on a page, without having to change any values ​​on that page. Simple implementation:

void trigger_cow(char* addr){
    *addr = *addr;
}

does not work because GCC optimizes the line. If I use volatile,

void trigger_cow(char* addr){
    volatile char* vaddr = (volatile char*) addr;
    *vaddr = *vaddr;
}

then it works under -O3.

Will this hack work under other compilers or optimization settings?

The description of volatile in most sites that I saw does not seem to describe what happens when you write to a volatile pointer, only what happens when you read it. Thanks!

+4
source share
3 answers

(§1.9/8) , :

:
- .

"" , , *vaddr = *vaddr; *vaddr, *vaddr.

+1

, volatile... , , , , ( ).

, * volatile .

* Itanium ABI, .

+4

volatile . .

++ " ", , .

+2

All Articles