_strset_s crashes
The following code crashes while executing _strset_s
I have given 80 as length in _strset_s
. What could be the problem?. I enabled run time stack frame checkb option /RTCs
char strToken[80];
_strs开发者_Python百科et_s(strToken, 80, '\0' );
You can let the compiler do the filling by using
char strToken[80] = {0};
This will zero all the bytes of the string.
The input to _strset_s
has to be null terminated according to MSDN. Since your string is not initialized to anything, it violates this invariant.
If str is a null pointer, or the size argument is less than or equal to 0, or the block passed in is not null-terminated, then the invalid parameter handler is invoked,
The default "invalid parameter handler" is to crash, again from MSDN:
The default invalid parameter invokes Watson crash reporting, which causes the application to crash and asks the user if they want to load the crash dump to Microsoft for analysis.
So I'd try Null terminating strToken first (or better yet do what Bo Persson suggests in his answer)
char strToken[80];
strToken[79] = '\0';
_strset_s(strToken, 80, '\0' );
精彩评论