L “String“ is not null terminated“ && 0 错误

本文探讨了在使用strcat函数连接字符串时出现的错误:“String”isnotnullterminated&&0。通过正确的内存分配与初始化解决了该问题。

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;

对申请的内存进行初始化。

ZW_API_C evxErrors cvxPartVarSubstituteResize ( char ** Str ) #include <zwapi_var.h> Substitute variable values into a string for all occurrences of variable references. The syntax for each variable reference is [$VAR] where VAR is the name of a variable in the active target. References for which the variable cannot be found are left in the string. This function is not limited to parts, but can be used when the active target is any type of root object in which variables may exist. The string will be reallocated (if necessary) to insert the variable values. It is not an error if "Str" is NULL or empty, nor is it an error if the string has no [$VAR] references, but it is an error if a variable specified in such a reference cannot be found in the active target. The string is reallocated if substituting the variable values produces a longer string. The string is not reallocated if it becomes shorter or stays the same length. If the block of memory used for the string is larger than needed for the string (i.e. larger than the number of bytes in the string plus 1), this function might make the block smaller. That is because reallocation requests only the amount of memory needed for the string, and might not consider unused space in the block. There is no need to allocate more memory than the string needs before calling this function. See alsocvxPartVarSubstitute() for a similar function that truncates a string instead of resizing it.Parameters [in,out] Str null-terminated string, may be NULL Return values succeeds •ZW_API_NO_ERROR fail •ZW_API_GENERAL_ERROR •ZW_API_ROOT_OBJ_ACT_FAIL • 翻译
07-13
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值