C ++ in MS Visual Studio 2008. Warning level 4 plus the addition of additional warnings. I expect this to give a warning at least , but most likely a compiler error?
The function declaration is as follows:
int printfLikeFunction( const int bufferLength, char * const buffer, const char * const format, ... );
Using the code is a typo: although ARRAY_SIZE outputBuffer is passed, outputBuffer itself - this should not be compiled:
printfLikeFunction( ARRAY_SIZE( outputBuffer ), "Format: %s, %s", arg1, arg2 );
Clearly, this is wrong and a mistake has been made. However, the compiler had to catch it! The buffer parameter must be char -pointer, and it receives a string literal, which is const char -pointer. It must be a mistake. (arg1 and arg2 are also (possibly const) char pointers, so the declaration match is consistent even though outputBuffer is in the right place).
At runtime, this code crashes when it tries to write to a string literal. Not surprisingly, I just don't understand how it is allowed to compile.
(Although, by the way, apparently, this is due to the fact that sprintf_s has buffer and size parameters in a different order for this function - it makes such errors uniquely unsuccessful).
c ++ visual-studio
Andy Patrick Oct 07 '09 at 8:44 2009-10-07 08:44
source share