Writing beyond the end of allocated memory (due to incorrect bounds calculation ) is an example of undefined behaviour
Writing beyond the end of allocated memory (due to incorrect bounds calculation ) is an example of undefined behaviour
[1] Which IMO goes back to C's original confusion of mixing up machine-level concepts with language-level concepts from the get-go, leaving optimizers no choice but unsound reasoning and blaming programmers when they get it wrong. Something something numerical loops and supercomputers.
As far as I'm aware, it's safe to use std::memcpy for this, and I believe compilers recognize the idiom (and will not actually emit code to perform a useless copy).
[1] https://en.cppreference.com/w/cpp/language/reinterpret_cast
Not all bugs are UB, you can have logic errors of course. But stack corruption is I believe always triggered by UB.