3 ms·
> For gcc and the target architecture that is compiling that code it is UB so it is optimizing things with that information For every C and C++ standards compl
by compiler-guy 2y ago
> For gcc and the target architecture that is compiling that code it is UB so it is optimizing things with that information
For every C and C++ standards compliant compiler it is UB. Every single one. By definition, whether or not the compiler then takes advantage of that. It may or may not be legal on the target machine in question, but C and C++ don't target the machine in question, they target an abstract machine with certain properties. The behavior of the abstract machine is then translated to behavior on the target machine. Dereferencing the nullptr on the abstract machine can result in any behavior at all, including what you describe here, but it is not promised by the standard.
If you want to access memory at address zero, you cannot do it in a defined way from within the language. You can drop to assembly or use compiler-defined extensions, but if you don't do it like that, you are playing with UB.