user-0 提问: 为什么下面这段代码debug时一切正常，运行的时候就崩掉。

回复:
user: user-1
content: 引用 28 楼 zhao4zhong1 的回复:13楼最后应选第二行。 纠正：最后应点按钮“中断”进入调试，按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。

user: user-1
content: 13楼最后应选第二行。

user: user-1
content: 13楼问题的解决方法： 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。

user: user-2
content: 学习了，谢谢各位大神了。

user: user-3
content: 还是sizeof好

user: user-0
content: 引用 23 楼 zhao4zhong1 的回复:8191+1=8192=默认tcp缓冲区大小的两倍。  strncpy, wcsncpy, _mbsncpy Copy characters of one string to another.  char *strncpy( char *strDest, const char *strSource, size_t count );  wchar_t *wcsncpy( wchar_t *strDest, const wchar_t *strSource, size_t count );  unsigned char *_mbsncpy( unsigned char *strDest, const unsigned char *strSource, size_t count );  Routine Required Header Compatibility  strncpy <string.h> ANSI, Win 95, Win NT  wcsncpy <string.h> or <wchar.h> ANSI, Win 95, Win NT  _mbsncpy <mbstring.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Each of these functions returns strDest. No return value is reserved to indicate an error.  Parameters  strDest  Destination string  strSource  Source string  count  Number of characters to be copied  Remarks  The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.  wcsncpy and _mbsncpy are wide-character and multibyte-character versions of strncpy. The arguments and return value of wcsncpy and _mbsncpy vary accordingly. These three functions behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tcsncpy strncpy _mbsnbcpy  wcsncpy     Example  /* STRNCPY.C */  #include <string.h> #include <stdio.h>  void main( void ) {    char string[100] = "Cats are nice usually";    printf ( "Before: %s\n", string );    strncpy( string, "Dogs", 4 );    strncpy( string + 9, "mean", 4 );    printf ( "After:  %s\n", string ); }   Output  Before: Cats are nice usually After:  Dogs are mean usually   String Manipulation Routines  See Also   _mbsnbcpy, strcat, strcmp, strcpy, strncat, strncmp, _strnicmp, strrchr, _strset, strspn 大概明白了，谢谢！ 是不是strncpy(ptchar, item,L-1);ptchar[L-1]=0这种表达方式是为了防止长度大于8191的时候才设计的？ 当长度小于等于8191的时候它和strncpy(ptchar, item,L);等价。 另外请您看一下在13楼我问您的问题，谢谢！

user: user-1
content: 8191+1=8192=默认tcp缓冲区大小的两倍。  strncpy, wcsncpy, _mbsncpy Copy characters of one string to another.  char *strncpy( char *strDest, const char *strSource, size_t count );  wchar_t *wcsncpy( wchar_t *strDest, const wchar_t *strSource, size_t count );  unsigned char *_mbsncpy( unsigned char *strDest, const unsigned char *strSource, size_t count );  Routine Required Header Compatibility  strncpy <string.h> ANSI, Win 95, Win NT  wcsncpy <string.h> or <wchar.h> ANSI, Win 95, Win NT  _mbsncpy <mbstring.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Each of these functions returns strDest. No return value is reserved to indicate an error.  Parameters  strDest  Destination string  strSource  Source string  count  Number of characters to be copied  Remarks  The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.  wcsncpy and _mbsncpy are wide-character and multibyte-character versions of strncpy. The arguments and return value of wcsncpy and _mbsncpy vary accordingly. These three functions behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tcsncpy strncpy _mbsnbcpy  wcsncpy     Example  /* STRNCPY.C */  #include <string.h> #include <stdio.h>  void main( void ) {    char string[100] = "Cats are nice usually";    printf ( "Before: %s\n", string );    strncpy( string, "Dogs", 4 );    strncpy( string + 9, "mean", 4 );    printf ( "After:  %s\n", string ); }   Output  Before: Cats are nice usually After:  Dogs are mean usually   String Manipulation Routines  See Also   _mbsnbcpy, strcat, strcmp, strcpy, strncat, strncmp, _strnicmp, strrchr, _strset, strspn

user: user-0
content: 引用 21 楼 zhao4zhong1 的回复:[quote=引用 20 楼 qq_21909183 的回复:] [quote=引用 19 楼 zhao4zhong1 的回复:] [quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } char *StringCopy(const char *item) { int L=__min(8191,strlen(item))+1; char *ptchar = (char *)malloc(L);//看看这句吧 strncpy(ptchar, item,L-1);ptchar[L-1]=0; return ptchar; }[/quote] 您的程序我有两点不太明白： 一是__min里的8191是什么意思 二是为什么不直接用strncpy(ptchar, item,L); 请您指教，谢谢！[/quote] 如果strlen(item)比8191大，只取item前8191个字节，多出来的忽略。 strncpy在处理刚好L-1个字节时，后面不加'\0',为确保ptchar的末尾有'\0',ptchar[L-1]=0;  __min Returns the smaller of two values.  type __min( type a, type b );  Routine Required Header Compatibility  __min <stdlib.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  The smaller of the two arguments  Parameters  type  Any numeric data type  a, b  Values of any numeric type to be compared  Remarks  The __min macro compares two values and returns the value of the smaller one. The arguments can be of any numeric data type, signed or unsigned. Both arguments and the return value must be of the same data type.  Example  /* MINMAX.C */  #include <stdlib.h> #include <stdio.h>  void main( void ) {    int a = 10;    int b = 21;     printf( "The larger of %d and %d is %d\n",  a, b, __max( a, b ) );    printf( "The smaller of %d and %d is %d\n", a, b, __min( a, b ) ); }   Output  The larger of 10 and 21 is 21 The smaller of 10 and 21 is 10   Floating-Point Support Routines  See Also   __max [/quote] 8191是某个具有特殊意义的值吗？ 为什么不是8192,8193？ 当item的长度不超过8191的时候，L正好是字符串长度加一，这时候用strncpy(ptchar, item,L);的话不是正好可以将结尾的\0也复制进去么，为什么还要分开处理？

user: user-1
content: 引用 20 楼 qq_21909183 的回复:[quote=引用 19 楼 zhao4zhong1 的回复:] [quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } char *StringCopy(const char *item) { int L=__min(8191,strlen(item))+1; char *ptchar = (char *)malloc(L);//看看这句吧 strncpy(ptchar, item,L-1);ptchar[L-1]=0; return ptchar; }[/quote] 您的程序我有两点不太明白： 一是__min里的8191是什么意思 二是为什么不直接用strncpy(ptchar, item,L); 请您指教，谢谢！[/quote] 如果strlen(item)比8191大，只取item前8191个字节，多出来的忽略。 strncpy在处理刚好L-1个字节时，后面不加'\0',为确保ptchar的末尾有'\0',ptchar[L-1]=0;  __min Returns the smaller of two values.  type __min( type a, type b );  Routine Required Header Compatibility  __min <stdlib.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  The smaller of the two arguments  Parameters  type  Any numeric data type  a, b  Values of any numeric type to be compared  Remarks  The __min macro compares two values and returns the value of the smaller one. The arguments can be of any numeric data type, signed or unsigned. Both arguments and the return value must be of the same data type.  Example  /* MINMAX.C */  #include <stdlib.h> #include <stdio.h>  void main( void ) {    int a = 10;    int b = 21;     printf( "The larger of %d and %d is %d\n",  a, b, __max( a, b ) );    printf( "The smaller of %d and %d is %d\n", a, b, __min( a, b ) ); }   Output  The larger of 10 and 21 is 21 The smaller of 10 and 21 is 10   Floating-Point Support Routines  See Also   __max

user: user-0
content: 引用 19 楼 zhao4zhong1 的回复:[quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } char *StringCopy(const char *item) { int L=__min(8191,strlen(item))+1; char *ptchar = (char *)malloc(L);//看看这句吧 strncpy(ptchar, item,L-1);ptchar[L-1]=0; return ptchar; }[/quote] 您的程序我有两点不太明白： 一是__min里的8191是什么意思 二是为什么不直接用strncpy(ptchar, item,L); 请您指教，谢谢！

user: user-1
content: 引用 2 楼 user-5 的回复:char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } char *StringCopy(const char *item) { int L=__min(8191,strlen(item))+1; char *ptchar = (char *)malloc(L);//看看这句吧 strncpy(ptchar, item,L-1);ptchar[L-1]=0; return ptchar; }

user: user-4
content: 一看到这代码我立刻就知道我不会

user: user-0
content: 引用 16 楼 user-5 的回复:你可以构造string的时候传递的字符串长点看看会不会调试的时候奔溃吧 已崩，谢谢提醒... 果然不崩是个巧合

user: user-5
content: 你可以构造string的时候传递的字符串长点看看会不会调试的时候奔溃吧

user: user-0
content: 引用 14 楼 user-5 的回复:这个不好说，只是调试时运气好，没破坏堆结构或者调试的时候有些额外的内存分配没有导致问题的产生 但这样的话有点奇怪，因为调试了很多次都没问题，运行的时候是次次都会崩掉，是调试和运行的原理不同？

user: user-5
content: 这个不好说，只是调试时运气好，没破坏堆结构或者调试的时候有些额外的内存分配没有导致问题的产生

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。 我不太明白调试方面的技巧，请您指教： 我开始的程序运行的时候会出现 几秒钟后变成 我点击调试会出现 我选择了第一个 就会跳出下面的界面 然后我就不知道怎么操作了，还请您说明一下，谢谢！

user: user-0
content: 引用 11 楼 user-5 的回复:[quote=引用 10 楼 qq_21909183 的回复:] [quote=引用 8 楼 qq_21909183 的回复:] [quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } 谢谢！ 改动这一句之后就可以了，不过这是为什么呢？ 还麻烦您解释一下，谢谢！[/quote] 是不是因为sizeof得到的是指针的大小...[/quote]是的[/quote] 那为什么在调试的时候这个错误不会引起程序崩溃呢...

user: user-5
content: 引用 10 楼 qq_21909183 的回复:[quote=引用 8 楼 qq_21909183 的回复:] [quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } 谢谢！ 改动这一句之后就可以了，不过这是为什么呢？ 还麻烦您解释一下，谢谢！[/quote] 是不是因为sizeof得到的是指针的大小...[/quote]是的

user: user-0
content: 引用 8 楼 qq_21909183 的回复:[quote=引用 2 楼 user-5 的回复:] char *StringCopy(const char *item) { 	char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧 	strcpy(ptchar, item); 	return ptchar; } 谢谢！ 改动这一句之后就可以了，不过这是为什么呢？ 还麻烦您解释一下，谢谢！[/quote] 是不是因为sizeof得到的是指针的大小...