简体   繁体   中英

Meaning of *(volatile unsigned int *) 0x00 = 0x00;

What is the meaning of the following line of code?

*(volatile unsigned int *) 0x00 = 0x00;

It is used in Assert definition:

void Assert()
{
    // (some other code above)

    *(volatile unsigned int *) 0x00 = 0x00;

    return;
}

I can guess it is trying to cause the program to crash with a segmentation fault or something similar.

Whoever wrote the code was thinking "I hope the compiler does not optimize this crash away, so let's put a volatile in front of the pointer dereference".

But all the code is doing is causing undefined behavior that is not guaranteed to crash.

An std::terminate() is a much better option if you want your program to terminate then that UB Assert()

Also note that an assert should be given a condition, and then if the condition is false the assert should trigger some failure code. Ot is not meant to be an "exit this program" function. See linked comment

Cast 0x00 to pointer to volatile unsigned int and then dereference it with operator * and write to that address 0x00. Which is undefined behavior and on most systems will cause crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM