Malloc、空闲和分段错误
我不明白为什么在这段代码中,对“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正确使用
malloc
分配内存:然后您执行以下操作:
这将导致
arr
指向字符串文字“xxxxxxx”
的地址,泄漏您的malloc
内存。并且对字符串文字的地址调用free
会导致未定义的行为。如果要将字符串复制到分配的内存中,请使用
strcpy
:You are allocating the memory using
malloc
correctly:then you do this:
this will cause
arr
point to the address of the string literal"xxxxxxx"
, leaking yourmalloc
ed memory. And also callingfree
on address of string literal leads to undefined behavior.If you want to copy the string into the allocated memory use
strcpy
as:char_arr_allocator()
的第三行清除了malloc()
结果,并将其替换为数据页中的一块静态内存。对此调用free()
会导致崩溃。使用
str[n]cpy()
将字符串文字复制到缓冲区。The third line of
char_arr_allocator()
wipes out yourmalloc()
result and replaces it with a chunk of static memory in the data page. Callingfree()
on this blows up.Use
str[n]cpy()
to copy the string literal to the buffer instead.当您在 C 中编写常量字符串(例如
"xxxxxx"
)时,会发生该字符串直接进入可执行文件的情况。当您在源代码中引用它时,它会被替换为指向该内存的指针。因此,您可以将“Treatingarr
as a number”这一行解读为:
其中该数字是一个地址。
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 lineTreating
arr
as a number as something like: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.您将
arr
设置为malloc()
的返回值,这是正确的。但随后您将其重新分配为指向字符串常量“xxxxxxx”
。因此,当您调用free()
时,您会要求运行时释放字符串常量,这会导致段错误。You are setting
arr
to the return value ofmalloc()
, which is correct. But you are then reassigning it to point at the string constant"xxxxxxx"
. So when you callfree()
, you're asking the runtime to free a string constant, which causes the seg fault.