使用 strcat 附加字符数组不起作用
有人可以告诉我这段代码有什么问题吗???
char sms[] = "gr8";
strcat (sms, " & :)");
Can some one tell me what's wrong with this code???
char sms[] = "gr8";
strcat (sms, " & :)");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
sms
是一个大小为4
1 的数组。并且您要附加更多的字符文字,这些文字将超出数组,因为数组最多可容纳4
个字符,而这些字符已被g、r、8、\0< 占用/代码>。
1.顺便问一下,为什么是 4?答案:因为末尾有一个空字符!
如果您提到如下所示的数组大小,那么您的代码是有效且定义良好的。
但是 C++ 为您提供了更好的解决方案:使用
std::string
作为:sms
is an array of size4
1. And you're appending more char literals, which is going outside of the array, as the array can accommodate at max4
chars which is already occupied byg, r, 8, \0
.1. By the way, why exactly 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.
But then C++ provides you better solution: use
std::string
as:是的,没有空间容纳额外的字符。
sms[]
仅分配足够的空间来存储初始化的字符串。使用 C++,更好的解决方案是:
Yes, there is no room for the extra characters.
sms[]
only allocates enough space to store the string that it is initialized with.Using C++, a much better solution is:
您正在将数据复制到未分配的内存中。
当您执行此操作时:
char sms[] = "gr8";
您将创建一个包含 4 个字符的 char 数组,即“gr8”加上字符串末尾的 0 字符。然后,您尝试使用
strcat
调用将额外的字符复制到数组中,超出数组末尾。这会导致未定义的行为,这意味着会发生不可预测的事情(程序可能会崩溃,或者您可能会看到奇怪的输出)。要解决此问题,请确保将字符复制到的数组足够大以包含所有字符,并且不要忘记末尾的 0 字符。
You're copying data into unallocated memory.
When you do this:
char sms[] = "gr8";
you create a char array with 4 characters, "gr8" plus the 0 character at the end of the string.Then you try to copy extra characters to the array with the
strcat
call, beyond the end of the array. 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.
在 C 语言中,数组不会自动增长。
sms
具有特定的长度(4,在本例中 - 三个字母和终止 NULL)。当您调用strcat
时,您试图将超过其长度的字符附加到该数组中。这是未定义的行为,并且会破坏您的程序。
相反,如果您分配了一个足够大的数组来包含两个字符串,那就没问题了:
C++ 对数组的限制(基本上)与 C 相同。然而,它提供了更高级别的设施,使您不必经常处理数组,例如 std::string :
这更好的原因是您不需要不必提前确切知道你的字符串有多长。 C++ 将为您增加内存中的底层存储。
In C, arrays don't automatically grow.
sms
has a specific length (4, in this case - three letters and the terminating NULL). When you callstrcat
, you are trying to append characters to that array past its length.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:
C++ has the (basically) the same restrictions on arrays that C does. 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
: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.
字符数组缓冲区溢出,然后在某处崩溃!
Buffer overflow for character array followed by crash somewhere!
您的短信缓冲区只有 4 个字符长。 strcat 将在其末尾复制 5 个以上的字符并损坏堆栈。
Your sms buffer is only 4 characters long. strcat will copy 5 more characters over the end of it and corrupt the stack.