C++ char 数组正确移动空终止符吗?
嗨,我的问题有点难以解释,所以我将在这里发布我的代码部分并用示例解释问题。
这里的代码有一个大数组和一个小数组,其中大数组被分成小部分,存储在小数组中,小数组在屏幕上输出其内容。
然后,我释放小数组分配的内存,并使用大数组的下一部分再次初始化它:
//this code is in a loop that runs until all of the big array has been copied
char* splitArray = new char[50];
strncpy(splitArray, bigArray+startPoint, 50); //startPoint is calculated with every loop run, it marks the next point in the array for copying
//output of splitArray on the screen here
delete splitArray;
//repeat loop here
现在我的问题是输出的字符串末尾每次都有一些随机符号。例如“some_characters_here...last_char_hereRANDOM_CHARS_HERE”。
深入研究后我发现splitArray的大小实际上不是50而是64,空终止符为64。 所以当我从 bigArray 复制到 splitArray 时,真实字符串后面仍然剩下 14 个随机字符,当然我不想输出它们。
一个简单的解决方案是手动将 splitArray 中的空终止符设置为 [50],但程序无法再次删除该数组。
有人可以帮我找到解决方案吗?最好有一些示例代码,谢谢。
Hi my problem is kind of difficult to explain so I'll just post my code section here and explain the problem with an example.
This code here has a big and a small array where the big array gets split up in small parts, is stored in the small array and the small array is outputting its content on the screen.
Afterwards I free the allocated memory of the small array and initialize it again with the next part of the big array:
//this code is in a loop that runs until all of the big array has been copied
char* splitArray = new char[50];
strncpy(splitArray, bigArray+startPoint, 50); //startPoint is calculated with every loop run, it marks the next point in the array for copying
//output of splitArray on the screen here
delete splitArray;
//repeat loop here
now my problem is that the outputted string has everytime some random symbols at the end. for example "some_characters_here...last_char_hereRANDOM_CHARS_HERE".
after looking deeper into it I found out that splitArray actually doesnt have a size of 50 but of 64 with the null terminator at 64.
so when I copy from bigArray into splitArray then there are still the 14 random characters left after the real string and of course I dont want to output them.
A simple solution would be to manually set the null terminator in the splitArray at [50] but then the program fails to delete the array again.
Can anybody help me find a solution for this? Preferably with some example code, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
刚刚设置了
splitArray[49] = 0
,程序怎么会“再次删除数组失败”呢?不要忘记,长度为 50 的数组的索引范围为 0 到 49。splitArray[50] = 0
正在写入分配给splitArray
的内存之外,其中所有所带来的后果。How does the program "fail to delete the array again" if you just set
splitArray[49] = 0
? Don't forget, an array of length 50 is indexed from 0 through 49.splitArray[50] = 0
is writing to memory outside that allocated forsplitArray
, with all the consequences that entails.当你为
splitArray
分配内存时,内存中没有填充NULL字符,你需要显式地这样做。因此,您的字符串没有正确以 NULL 结尾。为此,您可以执行char* splitArray = new char[51]();
在分配本身时使用 NULL 字符进行初始化(请注意,我分配 51 个字符以获得额外的 NULL 字符在最后)。 。另请注意,您需要执行delete[] splitArray;
而不是delete splitArray;
。When you allocate memory for the
splitArray
the memory is not filled with NULL characters, you need to explictly do it. Because of this your string is not properly NULL terminated. To do this you can dochar* splitArray = new char[51]();
to initialize with NULL character at the time of allocation itself (note that I am allocating 51 chars to have the extra NULL character at the end). . Also note that you need to dodelete[] splitArray;
and notdelete splitArray;
.函数
strncpy
的缺点是,如果源字符串包含超过 50 个字符,它不会终止目标字符串。看来你的情况确实如此!如果这确实是 C++,您可以使用 std::string splitArray(bigArray+startPoint, 50) 来完成。
The function
strncpy
has the disadvantage that it doesn't terminate the destination string, if the source string contains more than 50 chars. Seems like it does in your case!If this really is C++, you can do it with
std::string splitArray(bigArray+startPoint, 50)
.我发现您的代码存在一些问题:
new []
进行分配,则需要使用delete []
释放(而不是delete
>)你想要一些代码:
I see a couple of problems with your code:
new []
, you need to free withdelete []
(notdelete
)You wanted some code:
只要这样做就足够了:
我真的很想知道你为什么要这样做。这更干净:
它仍然执行复制,但为您处理(取消)分配和终止。您可以像这样获取底层字符指针:
它将正确地以 null 结尾,并且具有与字符串对象相同的生命周期(即,您不需要
free
或delete
)。注意。我没有更改您的原始代码,但丢失神奇的整数文字也是一个好主意。
Just doing this will be sufficient:
I'd really question why you're doing this anyway though. This is much cleaner:
it still does the copy, but handles (de)allocation and termination for you. You can get the underlying character pointer like so:
it'll be correctly nul-terminated, and have the same lifetime as the string object (ie, you don't need to
free
ordelete
it).NB. I haven't changed your original code, but losing the magic integer literals would also be a good idea.