L “String” is not null terminated" && 0 错误
参考 : https://www.codentalks.com/t/topic/1747
const char *stringOne = "Hello";
const char *stringTwo = " World!";
char *hello = new char[strlen(stringOne) + strlen(stringTwo) + 1];
strcat_s(hello, strlen(stringOne) + strlen(stringTwo) + 1, stringOne);
strcat_s(hello, strlen(stringOne) + strlen(stringTwo) + 1, stringTwo);
cout << hello << endl;
error:
strcat将两个字符串连接在一起。在您的例子中,问题是由于字符串没有初始化,“垃圾数据”在字符串中。当你不初始化它时,任何类型的随机数据都可能在你的字符串中,这就是你得到错误的原因
C has many string functions. One of them is strcat. strcat concatenates two strings together. The problem, in your case, was that since the string wasn’t initialized, “garbage data” was in your string. When you don’t initialize it, any kind of random data could be in your string and that was why you were getting the error. Your friend didn’t get the error just out of random luck. He just as easily could have gotten the error too. The string was probably allocated in an area of memory that just happen to be filled with 0’s (the null terminator).
const char *stringOne = "Hello";
const char *stringTwo = " World!";
char *hello = new char[strlen(stringOne) + strlen(stringTwo) + 1]();
strcat_s(hello, strlen(stringOne) + strlen(stringTwo) + 1, stringOne);
strcat_s(hello, strlen(stringOne) + strlen(stringTwo) + 1, stringTwo);
cout << hello << endl;
对申请的内存进行初始化。
本文探讨了在使用strcat函数连接字符串时出现的错误:“String”isnotnullterminated&&0。通过正确的内存分配与初始化解决了该问题。
5529





