I am testing Visual C ++ 10 optimization options and have found a rather interesting thing. All code here is compiled with / O 2.
In the following code:
int _tmain(int argc, _TCHAR* argv[]) { char buffer[1024] = {}; MessageBoxA( 0, buffer, buffer, 0 ); memset( buffer, 0, sizeof( buffer ) ); return 0; }
calling memset() before return excluded from machine code (I am checking the disassembly). This is perfectly reasonable - if after that there is no reading from buffer , then memset() useless, and if developers really want to overwrite the buffer, you can use SecureZeroMemory() .
However, in the following code:
int _tmain(int argc, _TCHAR* argv[]) { char buffer[1024] = {}; MessageBoxA( 0, buffer, buffer, 0 ); memset( buffer, 0, sizeof( buffer ) ); Sleep( 0 );
the memset() call is not resolved. This call does not affect the observed behavior and can be eliminated in the same way as in the first fragment.
It may be a compiler flaw or it may be useful in some way - I cannot solve it.
Why can I leave the memset() call in the machine code emitted for the second fragment?