gpt4 book ai didi

c - 无效的读取内存 - valgrind

转载 作者:太空狗 更新时间:2023-10-29 17:02:58 24 4
gpt4 key购买 nike

这是我的代码:我正在尝试获取结构的信息并深度复制该信息。但是,valgrind显示“无效读取”。我知道那是我读取释放的内存。我不知道为什么;有人能帮我解决吗?

代码

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

struct student
{
int id;
char *name;
int age;
};

void get_info(struct student *dest, struct student *src)
{
memcpy(dest,src,sizeof(struct student));
dest->name = strdup(src->name);
}

int main()
{
struct student foo;
foo.id = 1001;
foo.name = strdup("kevin");
foo.age = 18;

struct student bar;
get_info(&bar, &foo);

puts(bar.name);

free(foo.name);
free(bar.name);

return 0;
}

Valgrind 报告

valgrind --tool=memcheck --leak-check=full ./test
==2130== Memcheck, a memory error detector
==2130== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2130== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==2130== Command: ./test
==2130==
==2130== Invalid read of size 4
==2130== at 0x40B083B: ??? (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x40B04A4: strdup (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x80484B1: get_info (test.c:15)
==2130== by 0x80484F8: main (test.c:26)
==2130== Address 0x419902c is 4 bytes inside a block of size 6 alloc'd
==2130== at 0x4026775: malloc (vg_replace_malloc.c:291)
==2130== by 0x40B04AF: strdup (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x80484D8: main (test.c:22)
==2130==
==2130== Invalid read of size 4
==2130== at 0x40B083B: ??? (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x409ACE4: puts (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x8048504: main (test.c:28)
==2130== Address 0x4199064 is 4 bytes inside a block of size 6 alloc'd
==2130== at 0x4026775: malloc (vg_replace_malloc.c:291)
==2130== by 0x40B04AF: strdup (in /lib/tls/i686/cmov/libc-2.11.1.so)
==2130== by 0x80484B1: get_info (test.c:15)
==2130== by 0x80484F8: main (test.c:26)
==2130==
kevin
==2130==
==2130== HEAP SUMMARY:
==2130== in use at exit: 0 bytes in 0 blocks
==2130== total heap usage: 2 allocs, 2 frees, 12 bytes allocated
==2130==
==2130== All heap blocks were freed -- no leaks are possible
==2130==
==2130== For counts of detected and suppressed errors, rerun with: -v
==2130== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from 6)

最佳答案

我认为这实际上是来自 valgrind 的错误报告,您应该将其抑制。这最终是您系统上 C 库中的错误。

投诉是从 strdup() 调用的代码正在将偏移量为 4 字节的 4 字节读取到由 malloc() 分配的 6 字节 block 中.鉴于 "kevin" 占用 6 个字节,那么我相信 memcpy() 变体已被 strdup() 使用并已被捕获通过数据一次读取 4 个字节的行为。虽然它实际上可能是安全的,但从技术上讲,valgrind 的提示是正确的。但是,您对此无能为力——您的代码是无辜的,而系统库是有罪的。这就是压制的目的!

+---+---+---+---+---+---+...+...+
| k | e | v | i | n | \0| ? | ? |
+---+---+---+---+---+---+...+...+

快速复制利用了 malloc() 的数据是 4 字节(更可能是 8 字节)对齐的事实。它在一次操作中复制了 4 个字节 'k'、'e'、'v' 和 'i';然后它复制字符串的其他两个字节 ('n', '\0') 加上在第二个操作中技术上不属于分配空间的两个字节。在 32 位系统上,实际分配的最小值可能是 8 个字节;在 64 位机器上它往往是 16 字节。这意味着额外的两个字节是为内存分配保留的空间的一部分,但是 valgrind 报告代码正在复制分配空间之外是正确的。

关于c - 无效的读取内存 - valgrind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20810926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com