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

发布于 2024-11-06 15:38:53 字数 102 浏览 0 评论 0原文

有人可以告诉我这段代码有什么问题吗???

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

Can some one tell me what's wrong with this code???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乄_柒ぐ汐 2024-11-13 15:38:53

sms 是一个大小为 4 1 的数组。并且您要附加更多的字符文字,这些文字将超出数组,因为数组最多可容纳 4 个字符,而这些字符已被 g、r、8、\0< 占用/代码>。

1.顺便问一下,为什么是 4?答案:因为末尾有一个空字符!

如果您提到如下所示的数组大小,那么您的代码是有效且定义良好的。

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

但是 C++ 为您提供了更好的解决方案:使用 std::string 作为:

#include <string>  //must

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

sms is an array of size 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.

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.

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:

#include <string>  //must

std::string sms = "gr8";
sms += " & :)"; //string concatenation - easy and cute!
缱绻入梦 2024-11-13 15:38:53

是的,没有空间容纳额外的字符。 sms[] 仅分配足够的空间来存储初始化的字符串。

使用 C++,更好的解决方案是:

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

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:

std::string sms = "gr8";
sms += " & :)";
七堇年 2024-11-13 15:38:53

您正在将数据复制到未分配的内存中。

当您执行此操作时: 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.

妄司 2024-11-13 15:38:53

在 C 语言中,数组不会自动增长。

sms 具有特定的长度(4,在本例中 - 三个字母和终止 NULL)。当您调用 strcat 时,您试图将超过其长度的字符附加到该数组中。

这是未定义的行为,并且会破坏您的程序。

相反,如果您分配了一个足够大的数组来包含两个字符串,那就没问题了:

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

C++ 对数组的限制(基本上)与 C 相同。然而,它提供了更高级别的设施,使您不必经常处理数组,例如 std::string :

#include <string>

// ...

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

这更好的原因是您不需要不必提前确切知道你的字符串有多长。 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 call strcat, 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:

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

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:

#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.

被翻牌 2024-11-13 15:38:53

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

Buffer overflow for character array followed by crash somewhere!

两个我 2024-11-13 15:38:53

您的短信缓冲区只有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文