Why the piece of these code would crash sometime?
#define MAX_LENGTH_STRING 4096
void BugCode()
{
char szLine[MAX_LENGTH_STRING] = {0};
char szSelection[MAX_LENGTH_STRING] = {0};
va_list my_args;
szSelection[0]= 0xe7 ;
szSelection[1]= 0xac ;
szSelection[2]= 0xac ;
szSelection[3]= 0xe4 ;
开发者_如何学Go szSelection[4]= 0xb8 ;
szSelection[5]= 0x80 ;
szSelection[6]= 0xe5 ;
szSelection[7]= 0x8d ;
szSelection[8]= 0x95 ;
szSelection[9]= 0x00 ;
va_start(my_args, szSelection);
#ifdef WIN32
_vsnprintf(szLine, MAX_LENGTH_STRING-1, szSelection, my_args);
#else
vsnprintf(szLine, MAX_LENGTH_STRING - 1, szSelection, my_args);
#endif
}
The BugCode() would crash at _vsnprintf() But if I copy&paste these code to a simple project, it works well...
You're not using va_start
correctly. va_start
(and va_list
) can only be used for variadic function arguments. Using them for any other purpose is undefined behaviour.
精彩评论