free()后出现段错误,常见原因是什么?

发布于 2024-08-22 13:13:07 字数 280 浏览 8 评论 0原文

释放某个指针后,我遇到了分段错误:

free(studentDB->name);

我可以在没有任何错误或警告的情况下获取它的值:

printf("[DBG] studentDB->name: %s\n", studentDB->name);

但是,正如我所说,当我尝试释放它时,程序崩溃了。 free 命令导致分段错误的最常见原因是什么?

I get a segmentation fault after freeing a certain pointer:

free(studentDB->name);

I can get its value without any errors or warnings:

printf("[DBG] studentDB->name: %s\n", studentDB->name);

However, as I said, the program crashes when I try to free it. What are the most common causes for a free command leading to a segmentation fault?

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

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

发布评论

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

评论(7

怀中猫帐中妖 2024-08-29 13:13:07

如果您没有 malloc() 它,则无法 free() 它。 studentDB->name 从哪里来?

If you didn't malloc() it, you can't free() it. Where does studentDB->name come from?

白龙吟 2024-08-29 13:13:07

您可能已经对其进行了 free() 编辑,或者覆盖了缓冲区溢出块之前的 malloc 信息

You've probably either free()'ed it already, or overwritten the malloc info preceding the block with a buffer overrun

篱下浅笙歌 2024-08-29 13:13:07

通常在程序的其他地方会出现堆损坏。堆通常是连续的,堆管理器用标头包围堆块以跟踪块 - 如果您覆盖块的标头,则访问它是可以的,但 free 很可能会失败。

Usually heap corruption somewhere else in the program. The heap is usually continous and the heap manager surrounds heap blocks with headers to track the blocks- If you overwrite the header of the block, access to it is fine, but free is most likely to fail.

罗罗贝儿 2024-08-29 13:13:07

如果studentDB 为NULL,则访问studentDB 指针的name 成员也可能会出现段错误。

Could also be that accessing the name member of the studentDB pointer is a segfault if studentDB is NULL.

遇见了你 2024-08-29 13:13:07

free 的段错误可能是由于在未使用 malloc 分配或已被 free 分配的指针上调用它而引起的。

如果您发布了分配 studentDB->name 的代码,将会有所帮助。

A segfault from free can be caused by calling it on a pointer that was not allocated with malloc, or had been free'd already.

It would help if you posted the code where studentDB->name was allocated.

胡大本事 2024-08-29 13:13:07

studentDB->name 之前是否已分配?如果您没有为该字段分配内存,那么当您调用free时,很可能会出现段错误!请检查该字段并确保它已被 mallocd 或 strdupd。

或者堆上其他地方存在损坏,与此同时发生,因为您正确地指出您可以看到 name 的值...

希望这有帮助,
此致,
汤姆.

Has studentDB->name being allocated previously? If you did not allocate memory for that field, chances are when you called free, you end up with a seg-fault! Please check on that field and make sure it has being mallocd or strdupd.

Or that there is a corruption elsewhere on the heap, that coincided with this as you rightly pointed out you can see the value of name...

Hope this helps,
Best regards,
Tom.

森末i 2024-08-29 13:13:07

来自联机帮助页:

free( ptr ) 释放内存空间
ptr 指向的,它必须有
由先前的调用返回
malloc()、calloc() 或 realloc()。
否则,或者如果 free(ptr) 已经
之前被调用过,未定义
行为发生。如果 ptr 为 NULL,则不
执行操作。

还可以检查:

  1. studentDB 是否是一个指向包含成员“name”的类/结构的非 NULL 指针?
  2. StudentDB->name指向的空间是否是由malloc/calloc/realloc返回的?

From manpage:

free( ptr ) frees the memory space
pointed to by ptr, which must have
been returned by a previous call to
malloc(), calloc() or realloc().
Otherwise, or if free(ptr) has already
been called before, undefined
behaviour occurs. If ptr is NULL, no
operation is performed.

One can also check:

  1. Is studentDB a non-NULL pointer to a class/struct containing a member "name"?
  2. Is the space pointed to by studentDB->name was returned by malloc/calloc/realloc?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文