【发布时间】:2016-11-07 17:32:37
【问题描述】:
我正在尝试连接 char* 和 const 字符串,但系统挂起。以下是代码
unsigned long long size = 2*pow(10,18);
char* string = malloc(size);
strcat(string," ");
我不确定如何解决该错误。在 Windows 10 中使用 Dev c/c++。
编辑:感谢您的所有 cmets 和回答。我已经对我的代码进行了更改。但它仍然给出了同样的问题。 它不打印语句并且仍然挂起。
char* string = malloc(20*sizeof(char));
if(string=NULL){
printf("unsuccessful memory allocation");
}
strcat(string," ");
【问题讨论】:
-
检查
malloc()...if(string==NULL)printf("unsuccessful memory allocation");的返回值 -
然后
*string = 0;或复制strcat之前的字符串。 -
最可能的原因是你没有那么多内存,
malloc返回NULL。 -
这是一个非常大的内存请求。我什至没有那种容量的磁盘驱动器。
-
@denis 请记住不要编辑有问题的原始代码。这会导致在过时之前发布的答案和 cmets。