<div> </div><div> </div><blockquote><div><div><div>There are generally no absolute guarantees that the generated code is not optimized away - a future compiler may get smarter about memory lifetimes.</div></div></div></blockquote><div> </div><div>I didn't mention it initially because I figured everyone would already be aware and the original post revolved more around languages other than C/C++ where you do not necessarily have the same control over memory as you do there.</div><div> </div><div>However, memset_s()[1] is standardised and its entire purpose is precisely this case-- its guaranteed to not be optimised away. From memory, there is something similar in the upcoming C23 standard.</div><div> </div><div> </div><div>[1] <a href="https://en.cppreference.com/w/c/string/byte/memset" rel="noopener noreferrer" target="_blank">https://en.cppreference.com/w/c/string/byte/memset</a> "memset may be optimized away (under the as-if rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., gcc bug 8537). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes). This optimization is prohibited for memset_s: it is guaranteed to perform the memory write. Third-party solutions for that include FreeBSD explicit_bzero or Microsoft SecureZeroMemory."</div>