【问题标题】:Example Program for Error Message Address is not stack'd, malloc'd or (recently) free'd错误消息地址的示例程序未堆栈、malloc 或(最近)释放
【发布时间】:2019-08-21 04:56:05
【问题描述】:

作为学校的练习,我试图找到一个产生 valgrind 错误Address 0x1c is not stack'd, malloc'd or (recently) free'd 的程序。当然有很多关于这个错误的问题,但它们都是完整的程序,对于我的例子来说太大了。

您有什么建议可以告诉我这样的示例程序应该是什么样子吗?

感谢您的支持

【问题讨论】:

  • int *p = (int *) 0x1c; *p = 0;?
  • 为什么你需要这样的例子?您遇到的真正问题是什么?

标签: c malloc valgrind


【解决方案1】:

你总是可以作弊并明确释放这样的指针:

#include <stdint.h>
#include <stdlib.h>

int
main (void)
{
  free ((void *) (uintptr_t) 0x1c);
  return 0;
}

一个更实际的例子是一个空指针取消引用,涉及偏移量 28 处的结构成员。像这样:

#include <stddef.h>

struct data
{
  int pad[7];
  int value;
};

int
main (void)
{
  volatile struct data *volatile pointer = NULL;
  pointer->value = 0;
  return 0;
}

(需要volatile 关键字来防止编译器识别空指针引用死存储,并相应地优化。)

【讨论】:

    猜你喜欢
    • 2023-02-23
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多