当前位置: 代码迷 >> C语言 >> 请教一个程序死循环问题~~~~~高手请进!!!
  详细解决方案

请教一个程序死循环问题~~~~~高手请进!!!

热度:288   发布时间:2008-03-20 14:26:58.0
请教一个程序死循环问题~~~~~高手请进!!!
#include<stdio.h>
#include<string.h>
main()
{   char xx[]="1,2,3,4,5,0";
    strcat(xx,"6789");
    printf("%s\n",xx);

}

运行时出现死循环,持续输出1,2,3,4,5,06789
去掉0时可以正常运行,且出现abnormal program termination,其他就不行。
搜索更多相关的解决方案: include  termination  

----------------解决方案--------------------------------------------------------
char xx[15]="1,2,3,4,5,0";
越界.
----------------解决方案--------------------------------------------------------
越界是不是指xx[]的长度不够?
----------------解决方案--------------------------------------------------------
越界是不是指xx[]的长度不够?
----------------解决方案--------------------------------------------------------
yes
----------------解决方案--------------------------------------------------------
嘿。。问一下。 。。为什么超界。。。有没有定义宽度啊。麻烦解释一下好吗
----------------解决方案--------------------------------------------------------
printf("%d",strlen(xx));
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<string.h>
void main()
{   char xx[]="1,2,3,4,5,0";
    strcat(xx,"6789");
    printf("%s\n",xx);

}
不去0不是对的吗
怪了 xx[]中是5个数的时候就是错的 其他的都对的

[[it] 本帖最后由 zmhdxy 于 2008-3-20 16:18 编辑 [/it]]
----------------解决方案--------------------------------------------------------
Remarks
No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.
注意,没有这个函数没有检查内存边界。如果soure和destination存在内存重叠,结果是不可预测的。

Security Remarks
The first argument, strDestination, must be large enough to hold the current strDestination and strSource combined and a closing '\0'; otherwise, a buffer overrun can occur.
第一个参数的内存缓冲区,必须足够大以能够容纳原来字符串和你要追加的字符串以及结尾的\0。否则会出现内存溢出。
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]tyule168[/un] 在 2008-3-20 15:29 的发言:[/bo]

嘿。。问一下。 。。为什么超界。。。有没有定义宽度啊。麻烦解释一下好吗

虽然没有指定宽度,但在初始化的时候大小已经固定了
----------------解决方案--------------------------------------------------------
  相关解决方案