debug版本的CRT函数默认情况下会用0xFD字符进行填充,而Release版本则是用0填充,所以在使用时需要注意。当然可以用_CrtSetDebugFillThreshold函数进行修改默认设置,如_CrtSetDebugFillThreshold(0)可以禁止默认设置,都会用0填充。

  参考MSDN的描述:

 

Retrieves or modifies the threshold controlling buffer-filling behavior in debug functions.

size_t _CrtSetDebugFillThreshold(

   size_t _NewThreshold

);

Parameters

newThreshold

New threshold.

Return Value

The previous threshold.

Remarks

The debug versions of some security-enhanced CRT functions fill the buffer passed to them with a special character (0xFD). 

This helps to find cases where the incorrect size was passed to the function. Unfortunately, it also reduces performance. To 

improve performance, use _CrtSetDebugFillThreshold to disable buffer-filling for buffers larger than the threshold. A 

threshold of 0 will disable it for all buffers.

The default threshold is SIZE_T_MAX.

Here is a list of the affected functions.

strcpy_s, wcscpy_s, _mbscpy_s

strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

_mbsnbcpy_s, _mbsnbcpy_s_l

strcat_s, wcscat_s, _mbscat_s

strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

_mbsnbcat_s, _mbsnbcat_s_l

_strset_s, _strset_s_l, _wcsset_s, _wcsset_s_l, _mbsset_s, _mbsset_s_l

_strnset_s, _strnset_s_l, _wcsnset_s, _wcsnset_s_l, _mbsnset_s, _mbsnset_s_l

_mbsnbset_s, _mbsnbset_s_l

_makepath_s, _wmakepath_s

_splitpath_s, _wsplitpath_s

_strlwr_s, _strlwr_s_l, _mbslwr_s, _mbslwr_s_l, _wcslwr_s, _wcslwr_s_l

_strupr_s, _strupr_s_l, _mbsupr_s, _mbsupr_s_l, _wcsupr_s, _wcsupr_s_l

strerror_s, _strerror_s, _wcserror_s, __wcserror_s

_itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s

_ecvt_s

_fcvt_s

_gcvt_s

 
例子:
Example
  Copy Code 
// crt_crtsetdebugfillthreshold.cpp
// compile with: /MTd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>
 
void Clear( char buff[], size_t size )
{
   for( int i=0; i<size; ++i )
      buff[i] = 0;
}
 
void Print( char buff[], size_t size )
{
   for( int i=0; i<size; ++i )
      printf( "%02x  %c\n", (unsigned char)buff[i], buff[i] );
}
 
int main( void )
{
   char buff[10];
 
   printf( "With buffer-filling on:\n" );
   strcpy_s( buff, _countof(buff), "howdy" );
   Print( buff, _countof(buff) );
 
   _CrtSetDebugFillThreshold( 0 );
 
   printf( "With buffer-filling off:\n" );
   Clear( buff, _countof(buff) );
   strcpy_s( buff, _countof(buff), "howdy" );
   Print( buff, _countof(buff) );
}
 
  Copy Code 
With buffer-filling on:
68  h
6f  o
77  w
64  d
79  y
00
fd  ²
fd  ²
fd  ²
fd  ²
With buffer-filling off:
68  h
6f  o
77  w
64  d
79  y
00
00
00
00
00