*(char*)0 = 0; – What does the C++ programmer intend with this code? [video]
Before I watch the full talk for the answer (what I&#x27;ve seen so far, he states there&#x27;s not a right answer)...<p>I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again.<p>Later, you probably want to detect the presence of null pointers by checking if &amp;maybeAStruct is either 0 or valid. If you accidentally test the value at that address, you&#x27;ll get a non-zero and then run code you didn&#x27;t intend to run. By defining address 0x0 as value 0, you&#x27;ll avoid this issue. That has two problems.<p>Alternatively, you define address 0x0 to erase the initial stack pointer so that a nefarious actor can&#x27;t somehow memory dump and then find out where the program starts its execution. I don&#x27;t see the benefit for several reasons.<p>-----<p>Scenario 1.<p>The first problem is that you shouldn&#x27;t write code poorly. If you meant to check a variable&#x27;s address and instead check its value, that&#x27;s on you, your test suite, your compiler, and your debugger (and by extension, your colleagues and educators).<p>Second, writing to address 0x0 on a Cortex usually involves writing to flash... which is not a temporary&#x2F;nonpersistent change, more involved than simply writing to an address (you usually need to enable the flash controller and set some peripheral registers), and would usually not be allowed on a production device because that&#x27;s where the bootloader is or that section of memory has been write protected following good practices.<p>Scenario 2.<p>The more I think about this one, the less it makes sense. Anyone with debug port access or a memory dump is already inside the castle, well past any defenses e.g. zeroing the main stack pointer. Plus, the same caveats from Scenario 1 apply: if you COULD write to 0x0, it is almost always a terrible idea.<p>-----<p>Grand takeaway? This is probably a talk relevant to PC and not embedded. A Cortex-M would definitely complain about this code. Even on an AVR (atmega) changing 0x0 would be changing R0. I don&#x27;t even know if that register is directly writeable during execution---probably because it&#x27;s how you set some bootloader bits---but it&#x27;s definitely dangerous.<p>With that in mind, I searched for &quot;virtual address space windows&quot; to figure out what goes in an executable at address 0x0. After reading Microsoft&#x27;s first two articles, it&#x27;s still unclear what goes in 0x0. Wikipedia? No answer. Next result? Nothing.<p>Finally, a page about Windows ME&#x2F;98&#x2F;95 states address 0x0 is &quot;available to the process&quot; but is &quot;not writable&quot;.<p>After ten more minutes of fruitless searching, I&#x27;m willing to just test what happens, because I expect that&#x27;s not even writable without side effects during execution.<p><pre><code>    #include &lt;iostream&gt;
    int main() {
        *(char*)0 = 0;
        std::cout &lt;&lt; &quot;test&quot; &lt;&lt; std::endl;
    }
</code></pre>
and then, &quot;g++ test.cpp -o test&quot;, and ...<p><i>[1]    869 segmentation fault  .&#x2F;test</i><p>Linux? Fail.<p>-----<p>&quot;cl test.cpp &#x2F;link &#x2F;out:test.exe&quot;...<p>This compiles and runs but does not display the test string. (If you remove the 0x0 assignment, the string displays; no surprise)<p>Interestingly, I can&#x27;t delete test.exe right away. It has stayed open:<p><i>ERROR: The process with PID 12776 (child process of PID 19728) could not be terminated.</i><p>Not only does PID 19728 not exist, I have to use an administrator console to &quot;taskkill test.exe&quot;, so this would probably never belong inside user space code on Windows.<p>-----<p>Thus, this short code snippet seems like trouble, and the answer I would hope for as an interviewer is, &quot;yeah, I don&#x27;t write code like that, sorry.&quot;<p>(Coincidentally, I would say the same in response to the professors giving microcontroller exam questions that try to stump students with multiple casts and dereferences in a single line *ahem Moreno*. Stop teaching students methods they will never, ever use in a shop that cares about its code!!)<p>Now I watch and learn.