调用 free() 抛出分段错误
我有下面的代码引发分段错误。请建议对此可以采取什么措施。
#include <stdio.h>
int main() {
char *p ,*q ;
p =(char *)malloc(20) ;
*p = 30 ;
p++ ;
p=q ;
free(q) ;
return 0;
}
谢谢
I have the below code which is throwing a segmentation fault. Please suggest what can be done to this.
#include <stdio.h>
int main() {
char *p ,*q ;
p =(char *)malloc(20) ;
*p = 30 ;
p++ ;
p=q ;
free(q) ;
return 0;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在释放
malloc
未返回的内容(q
从未初始化)。我还可以看到你正在做的事情在这样做时,你正在失去
p
并且你不能再释放它了。以防万一您的意思是q=p
,这也无效。您只能释放malloc
返回的内容。编辑
根据评论,OP似乎确实打算
q = p
。你可以这样做:我看到你在问一些关于 char 与 integer 的问题。两者是一样的:
您只能
释放
由malloc
返回的确切值。You are freeing something not returned by
malloc
(q
is never initialized). Also I can see you are doingIn doing this, you are losing
p
and you can't free it anymore. Just in case you meantq=p
, that's not valid either. You can only free whatmalloc
returned.EDIT
In light of comment it seems the OP does intend
q = p
. You can do this:I see you are asking something about char versus integer. It's the same:
You can only
free
the exact values returned bymalloc
.您从未初始化要释放的
q
指针,这就是您的程序出现段错误的原因:它尝试释放
随机未初始化的指针。You never initialize that
q
pointer you're freeing, that's why your program segfaults: it tries tofree
a random uninitialized pointer.您没有将 q 初始化到您 malloc 的区域,然后您也丢弃了 p (执行 p = q)。您需要将指针保存到 malloc 区域以便能够释放它。所以像
You do not initialize q to the area you malloc-ed, and then you trash p (doing p = q), too. You need to save your pointer to malloc-ed area in order to be able to free it. So something like
你正在释放q,它指向一个未定义的地址。 编写,
您可能需要的是在递增 p
( p++; )
之前以便释放 q 对您的所有代码都有意义(保留原始指针以释放它)
you are freeing q, that point to an undefined address. What you probably need is to write
before incrementing p
( p++; )
so freing q will make sense with all your code ( preserving the original pointer for freeing it )
我之前告诉过你的原因,现在让我更清楚地告诉你。记住这些 arpita,free() 不适用于普通指针,它只适用于与 malloc()、calloc() 和 realloc( )。 free() 的开发方式仅适用于动态内存分配概念。如果您尝试将它与普通指针一起使用,则会在基于 Linux 的操作系统中导致分段错误或核心转储,这是因为该函数是在那样因为你正在尝试释放 q 指向的内存,但 q 未初始化,您不知道它指向的地址,甚至不知道要释放的内存大小。所以他们的错误在于。我希望您理解。还有一件事
人们可能会说
int main()
{
int a=45;
int *q=&a;
自由(q);
分配
有些人可能会想并问上面的问题,说上面的代码中的 q 已初始化,它指向 a,现在为什么 free() 不起作用。因为我已经告诉过你 free() 仅适用于动态内存 功能。在上面,您指向 int a,但请注意 int a 是一个静态分配的变量。因此错误仍然存在。
然而,当您转移到其他语言时,即使它不涉及动态分配,您也会有删除运算符来删除它。人们给我带来了上述代码有效的通知。我对某些编译器说是的,但编译器不适用于真正的应用程序。我建议您使用 gcc 编译器,
The reason I told you before but now let me tell you more clearly.Remember these arpita, the free() does not work with normal pointers , it only works with the pointers that are involved with malloc(),calloc() and realloc().The free() was developed in a way that it works only with dynamic memory allocation concepts.If you try to use it with a normal pointer it results segmentation fault or core dumped in linux based OS it is because the function is defined in that way because you are trying to free a memory pointed by q but q is uninitialized you don't know the address it is pointing to and you don't even know the size of memory to be deallocate.So their lies the error.I hope you understood. And one more thing
people might be saying
int main()
{
int a=45;
int *q=&a;
free(q);
}
Some people may think and ask the above question saying that the q is initialized in the above code it is pointing to a and now why free() is not woking.Because i already told you the free() works only with dynamic memory allocation function. And in the above you are pointing to int a, but notice that int a, is a statically allocated variable. Hence still the error exists.
However when you move to some other language you will be having delete operator to delete it even if it not involved in dynamic allocation.People bring me a notification that the above code works . and i say yes with some compilers but that compilers are not for the real applications.I suggest you to work on gcc compiler,