Malloc、空闲和分段错误

发布于 2024-09-26 07:36:21 字数 670 浏览 4 评论 0原文

我不明白为什么在这段代码中,对“free”的调用会导致分段错误:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *char_arr_allocator(int length);

int main(int argc, char* argv[0]){

    char* stringa =  NULL;
    stringa = char_arr_allocator(100);  
    printf("stringa address: %p\n", stringa); // same address as "arr"
    printf("stringa: %s\n",stringa);
    //free(stringa);

    return 0;
}

char *char_arr_allocator(int length) {
    char *arr;
    arr = malloc(length*sizeof(char));
    arr = "xxxxxxx";
    printf("arr address: %p\n", arr); // same address as "stringa"
    return arr;
}

有人可以向我解释一下吗?

谢谢, 塞戈拉斯

I don't understand why, in this code, the call to "free" cause a segmentation fault:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *char_arr_allocator(int length);

int main(int argc, char* argv[0]){

    char* stringa =  NULL;
    stringa = char_arr_allocator(100);  
    printf("stringa address: %p\n", stringa); // same address as "arr"
    printf("stringa: %s\n",stringa);
    //free(stringa);

    return 0;
}

char *char_arr_allocator(int length) {
    char *arr;
    arr = malloc(length*sizeof(char));
    arr = "xxxxxxx";
    printf("arr address: %p\n", arr); // same address as "stringa"
    return arr;
}

Can someone explain it to me?

Thanks,
Segolas

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

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

发布评论

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

评论(4

嘿嘿嘿 2024-10-03 07:36:21

您正确使用malloc分配内存:

arr = malloc(length*sizeof(char));

然后您执行以下操作:

arr = "xxxxxxx";

这将导致arr指向字符串文字“xxxxxxx”的地址,泄漏您的malloc内存。并且对字符串文字的地址调用free会导致未定义的行为。

如果要将字符串复制到分配的内存中,请使用strcpy

strcpy(arr,"xxxxxxx");

You are allocating the memory using malloc correctly:

arr = malloc(length*sizeof(char));

then you do this:

arr = "xxxxxxx";

this will cause arr point to the address of the string literal "xxxxxxx", leaking your malloced memory. And also calling free on address of string literal leads to undefined behavior.

If you want to copy the string into the allocated memory use strcpy as:

strcpy(arr,"xxxxxxx");
假装不在乎 2024-10-03 07:36:21

char_arr_allocator() 的第三行清除了 malloc() 结果,并将其替换为数据页中的一块静态内存。对此调用 free() 会导致崩溃。

使用 str[n]cpy() 将字符串文字复制到缓冲区。

The third line of char_arr_allocator() wipes out your malloc() result and replaces it with a chunk of static memory in the data page. Calling free() on this blows up.

Use str[n]cpy() to copy the string literal to the buffer instead.

她说她爱他 2024-10-03 07:36:21

当您在 C 中编写常量字符串(例如 "xxxxxx")时,会发生该字符串直接进入可执行文件的情况。当您在源代码中引用它时,它会被替换为指向该内存的指针。因此,您可以将“Treating arr as a number”这一行解读

 arr = "xxxxxxx";

为:

 arr = 12345678;

其中该数字是一个地址。 malloc 返回了一个不同的地址,当您将新地址分配给 arr 时,您将其丢弃。您遇到了段错误,因为您试图释放直接位于可执行文件中的常量字符串 - 您从未分配它。

When you write a constant string in C, such as "xxxxxx", what happens is that that string goes directly into the executable. When you refer to it in your source, it gets replaced with a pointer to that memory. So you can read the line

 arr = "xxxxxxx";

Treating arr as a number as something like:

 arr = 12345678;

Where that number is an address. malloc has returned a different address, and you threw that away when you assigned a new address to arr. You are getting a segfault because you are trying to free a constant string which is directly in your executable -- you never allocated it.

記憶穿過時間隧道 2024-10-03 07:36:21

您将 arr 设置为 malloc() 的返回值,这是正确的。但随后您将其重新分配为指向字符串常量“xxxxxxx”。因此,当您调用 free() 时,您会要求运行时释放字符串常量,这会导致段错误。

You are setting arr to the return value of malloc(), which is correct. But you are then reassigning it to point at the string constant "xxxxxxx". So when you call free(), you're asking the runtime to free a string constant, which causes the seg fault.

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