连接字符串后出现 Malloc() 内存损坏错误

发布于 2024-11-04 14:01:16 字数 514 浏览 1 评论 0原文

伙计们,我正在生成一个字符串,它表示文件的路径,连接宏和字符串。函数是这样的:

char *userPath(char *username)
{
   char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));
   path[0] = '\0';
   strcat(path, MAILBOXES);
   strcat(path, "/");
   strcat(path, username);
   return path;
}

返回的指针引用了正确的字符串,但是在调用该函数之后,该过程抛出了一个非常非常糟糕的* glibc检测到的。/ mmboxd: malloc(): 内存损坏: 0x085310a8 ** 带有相对回溯。我知道问题就在这里,因为我在实现它后就开始出现此错误,而且因为我使用的唯一 malloc 就在这里。这段代码有什么问题吗?

Guys I'm generating a string which rappresent a path to a file, concatenating a macro and a string. The function is this:

char *userPath(char *username)
{
   char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));
   path[0] = '\0';
   strcat(path, MAILBOXES);
   strcat(path, "/");
   strcat(path, username);
   return path;
}

The returned pointer reference a correct string, but after some call to this function the process throws out a very very bad * glibc detected ./mmboxd: malloc(): memory corruption: 0x085310a8 ** with the relative backtrace. I know it's here the problem, since I started having this error once implemented it, and also because the only malloc I use is here. What's wrong with this piece of code?

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

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

发布评论

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

评论(8

拥抱我好吗 2024-11-11 14:01:16

考虑到您添加的分隔符和空终止符,+1 应为 +2。并且您可以省略 sizeof(char),它始终为 1。

The +1 should be +2 to take into account the separator you add and the null terminator. And you can omit sizeof(char), which will always be 1.

夕嗳→ 2024-11-11 14:01:16

问题是:

   char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));

您为 a) MAILBOXES 中的所有字符、b) username 中的所有字符以及 c) '/ 分配了足够的内存' 字符,但您忘记了 d) 终止 '\0' 字符!所以 + 1 应该是 + 2

您的代码还有一些其他奇怪之处,但它们并没有错,只是可以做得更好:

  1. 您不需要在 C 中转换 malloc 的返回值,有些人(比如我)认为这种风格很糟糕,原因有多种,你完全有能力进行谷歌搜索。
  2. sizeof(char) 始终为 1(这是在标准中定义的)。有人说要保持对称。有人说,既然是一个,就把它拿出来吧。有人说将其更改为 sizeof *path,这样,如果将 path 更改为 wchar_t *malloc 将正确调整以保持分配正确的尺寸。
  3. 使用 strcat 将第一位数据写入字符串可能效率较低。为什么不删除 path[0] = '\0'; 行并仅使用 strcpy 作为数据的第一位?
  4. 您计算所有字符串的长度,但随后将它们丢弃并使用 strcat,它将重新遍历(之前计算的)长度以找到正确的位置。如果您存储了两次 strlen 调用的结果,则无需使用 strcat 并不必要地重新计算字符串结尾的位置。
  5. 使用 strcat 附加单个字符效率低下。
  6. 在使用 malloc 之前,您不会检查它的返回值是否成功。

Here's the problem:

   char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));

You're allocating enough memory for a) all the characters in MAILBOXES, b) all the characters in username, and c) the '/' character, but you're forgetting d) the terminating '\0' character! So + 1 should be + 2

There are a few other oddities about your code, but they're not wrong, just things that could be better:

  1. You don't need to cast the return value of malloc in C, and some (like me) consider it bad style for various reasons that you're more than capable of Googling.
  2. sizeof(char) is always 1 (this is defined in the standard). Some people say to keep it in for symmetry. Some say take it out since it's one. Some say change it to sizeof *path, so that if you change path to a wchar_t * the malloc will correctly adjust to keep allocating the right size.
  3. Using strcat to write the first bit of data to a string is potentially inefficient. Why not drop the path[0] = '\0'; line and just use strcpy for the first bit of data?
  4. You calculate the lengths of all of the strings, but then you throw them away and use strcat, which will re-traverse the (previously calculated) lengths to find the right spot. If you stored the results of your two strlen calls, you wouldn't need to use strcat and unnecessarily keep recalculating where the end of the string is.
  5. Using strcat to append a single character is inefficient.
  6. You don't check the return value of malloc for success or failure before you use it.
雪若未夕 2024-11-11 14:01:16

您似乎没有为零终止符留出空间。您应该为此分配一个额外的char

我假设 malloc() 中的 +1 用于路径分隔符。将其设置为 +2,这样您就有空间容纳终止空字符。

You don't appear to have allowed space for the zero-terminator. You should be allocating an extra char for that.

I'm assuming that the +1 in the malloc() is for the path separator. Make it +2 and you'll have space for the terminating null character.

雾里花 2024-11-11 14:01:16

当您分配“路径”字符串时,您忘记添加在 MAILBOXES 和用户名之间添加的“/”字符的长度。

When you allocate the "path" string you forgot to add the length of the "/" char that you add between MAILBOXES and username.

ヤ经典坏疍 2024-11-11 14:01:16

看来您需要分配另一个字节以实现零终止。

It appears you would need to malloc an another byte for zero termination.

别再吹冷风 2024-11-11 14:01:16

您需要为空字符“\x00”分配一个额外的字节作为 C 字符串中的字符串终止符。

目前,您只为 / 字符分配一个额外字节。

所以尝试+2而不是+1

You need to allocate one extra byte for null character "\x00" as the string terminator in C strings.

Currently you allocate only one extra byte for / character.

So try +2 instead of +1

痴情 2024-11-11 14:01:16

您没有在 char 中为终止 null 进行预算。你的 malloc 长度应该是 +2,而不是 +1。

You're not budgeting in a char for the terminating null. Your malloc length should be +2, not +1.

拥抱没勇气 2024-11-11 14:01:16

malloc() 末尾的 +1/。但结尾处的空字符需要空间,该空间是由 strcat() 添加的。所以它是+2

char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 2));

Your +1 in the end of malloc() accounts for the /. But you need space for the null character at the end, which is added by strcat(). So it's a +2.

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