简体   繁体   English

使用strcat追加字符数组不起作用

[英]Appending character arrays using strcat does not work

Can some one tell me what's wrong with this code??? 有人可以告诉我这段代码有什么问题吗???

char sms[] = "gr8";  
strcat (sms, " & :)");

sms is an array of size 4 1 . sms是一个大小为4 1的数组。 And you're appending more char literals, which is going outside of the array, as the array can accommodate at max 4 chars which is already occupied by g, r, 8, \\0 . 并且你要追加更多的字符文字,这些字符串会超出数组,因为数组最多可容纳4字符,这些字符已被g, r, 8, \\0占用。

1. By the way, why exactly 4? 顺便问一下,为什么4呢? Answer : Because that there is a null character at the end! 答:因为最后有一个空字符!

If you mention the size of array as shown below, then your code is valid and well-defined. 如果你提到如下所示的数组大小,那么你的代码是有效且定义良好的。

char sms[10] = "gr8";  //ensure that size of the array is 10
                       //so it can be appended few chars later.
strcat (sms, " & :)");

But then C++ provides you better solution: use std::string as: 但是C ++为您提供了更好的解决方案:使用std::string作为:

#include <string>  //must

std::string sms = "gr8";
sms += " & :)"; //string concatenation - easy and cute!

Yes, there is no room for the extra characters. 是的,额外的角色没有空间。 sms[] only allocates enough space to store the string that it is initialized with. sms[]仅分配足够的空间来存储初始化的字符串。

Using C++, a much better solution is: 使用C ++,更好的解决方案是:

std::string sms = "gr8";
sms += " & :)";

You're copying data into unallocated memory. 您正在将数据复制到未分配的内存中。

When you do this: char sms[] = "gr8"; 当你这样做: char sms[] = "gr8"; you create a char array with 4 characters, "gr8" plus the 0 character at the end of the string. 你创建一个包含4个字符的字符数组,“gr8”加上字符串末尾的0字符。

Then you try to copy extra characters to the array with the strcat call, beyond the end of the array. 然后尝试使用strcat调用将额外的字符复制到数组中,超出数组的末尾。 This leads to undefined behaviour, which means something unpredictable will happen (the program might crash, or you might see weird output). 这会导致未定义的行为,这意味着会发生不可预测的事情(程序可能会崩溃,或者您可能会看到奇怪的输出)。

To fix this, make sure that the array that you are copying the characters to is large enough to contain all the characters, and don't forget the 0 character at the end. 要解决此问题,请确保要复制字符的数组足以包含所有字符,并且不要忘记最后的0字符。

In C, arrays don't automatically grow. 在C中,数组不会自动增长。

sms has a specific length (4, in this case - three letters and the terminating NULL). sms具有特定的长度(在这种情况下为4个字母,终止为NULL)。 When you call strcat , you are trying to append characters to that array past its length. 当你调用strcat ,你试图将字符附加到超过其长度的数组。

This is undefined behavior, and will break your program. 这是未定义的行为,会破坏您的程序。

If instead you had allocated an array with a large enough size to contain both strings, you would be okay: 如果您已经分配了一个足够大的数组来包含两个字符串,那么你可以:

char sms[9] = "gr8";
strcat (sms, " & :)");

C++ has the (basically) the same restrictions on arrays that C does. C ++对C的数组(基本上)有相同的限制。 However, it provides higher level facilities that make it so you don't have to deal with arrays a lot of the time, such as std::string : 但是,它提供了更高级别的工具,因此您不必在很多时候处理数组,例如std::string

#include <string>

// ...

std::string sms = "gr8";
sms += " & :)";

The reason this is nicer is that you don't have to know ahead of time exactly how long your string will be. 这个更好的原因是你不必提前知道你的字符串有多长。 C++ will grow the underlying storage in memory for you. C ++将为您增加内存中的底层存储。

字符数组的缓冲区溢出 ,然后崩溃到某处!

Your sms buffer is only 4 characters long. 你的短信缓冲区长度只有4个字符。 strcat will copy 5 more characters over the end of it and corrupt the stack. strcat将在其末尾复制5个以上的字符并破坏堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM