On a formal point of view, you are just invoking Undefined Behaviour because you are trying to access a BYTE *
as a A *
. Per the strict aliasing rule, it is enough for the compiler to to anything it wants.
In the real world, it will depend on the implementation, and it is likely to give correct results. Under the hood, common implementations for vector
use allocated memory for their data
member. That means that:
- it will be correctly aligned for any type
- the actual allocated memory is likely to be at least enough for an int (because of the alignment, it is at least 4 bytes on a 32 bits system and 8 on a 64 one)
- you can safely change the type of the allocated memory
That means that it is not surprising that this code does not crash, even if it is plain Undefined Behaviour.
Note: except in very special cases, and with notices in red flashing font, you should never rely on implementation detail for your code, because it can crash with next version of your compiler, or simply if you change build options.
The heap corruption is only a warning, because your implementation has put a mark after the last byte that it has given to you, and has found that mark overwritten. But on a low level point of view you have not erased anything of major importance - assuming your implementation is like mine :-)