gpt4 book ai didi

c - 免费 : Invalid Size : Valgrind

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:23 32 4
gpt4 key购买 nike

我想使用 valgrind 释放一个 malloc:

我收到这个错误:

MCM : 1900
free(): invalid size
Abandon (core dumped)

这是我编译的代码:

char test[6][10] = {"MCM", "MCML", "MCMLXXXV", "MCMXC", "MM", "MMVI"};
int size[] = {3, 4, 8, 5, 2, 4};

for (int i = 0; i < 6; i++)
{
char *a = malloc(sizeof(char) * size[i]);
a = test[i];
int result = convert(a, roman, arabe, size[i]);
printf("%s : %d\n",a ,result);
free(a);
}

并通过使用:

valgrind --leak-check=full ./roman

我收到这条消息:

==23809== Memcheck, a memory error detector
==23809== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==23809== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==23809== Command: ./roman
==23809==
MCM : 1900
==23809== Invalid free() / delete / delete[] / realloc()
==23809== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23809== by 0x108B45: main (roman.c:76)
==23809== Address 0x1fff0003d0 is on thread 1's stack
==23809== in frame #1, created by main (roman.c:47)
==23809==
MCML : 1950
MCMLXXXV : 1985
MCMXC : 1990
MM : 2000
MMVI : 2006
MCM = 1900
MCML = 1950
MCMLXXXV = 1985
MCMXC = 1990
MM = 2000
MMVI = 2006
==23809==
==23809== HEAP SUMMARY:
==23809== in use at exit: 26 bytes in 6 blocks
==23809== total heap usage: 7 allocs, 7 frees, 1,050 bytes allocated
==23809==
==23809== 26 bytes in 6 blocks are definitely lost in loss record 1 of 1
==23809== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23809== by 0x108ABE: main (roman.c:72)
==23809==
==23809== LEAK SUMMARY:
==23809== definitely lost: 26 bytes in 6 blocks
==23809== indirectly lost: 0 bytes in 0 blocks
==23809== possibly lost: 0 bytes in 0 blocks
==23809== still reachable: 0 bytes in 0 blocks
==23809== suppressed: 0 bytes in 0 blocks
==23809==
==23809== For counts of detected and suppressed errors, rerun with: -v
==23809== ERROR SUMMARY: 7 errors from 2 contexts (suppressed: 0 from 0)

据我所知,有四个部分:

==23809== Invalid free() / delete / delete[] / realloc()
==23809== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23809== by 0x108B45: main (roman.c:76)
==23809== Address 0x1fff0003d0 is on thread 1's stack
==23809== in frame #1, created by main (roman.c:47)
==23809==

我看到我的错误是第 76 行:

free(a)

我不知道怎么解决。

而且我不太了解 HEAP SUMMARY 和 LEAK SUMMARY:它对应什么?堆总结:

==23809== HEAP SUMMARY:
==23809== in use at exit: 26 bytes in 6 blocks
==23809== total heap usage: 7 allocs, 7 frees, 1,050 bytes allocated
==23809==
==23809== 26 bytes in 6 blocks are definitely lost in loss record 1 of 1
==23809== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23809== by 0x108ABE: main (roman.c:72)

泄漏摘要:

==23809== LEAK SUMMARY:
==23809== definitely lost: 26 bytes in 6 blocks
==23809== indirectly lost: 0 bytes in 0 blocks
==23809== possibly lost: 0 bytes in 0 blocks
==23809== still reachable: 0 bytes in 0 blocks
==23809== suppressed: 0 bytes in 0 blocks

结论:

==23809== For counts of detected and suppressed errors, rerun with: -v
==23809== ERROR SUMMARY: 7 errors from 2 contexts (suppressed: 0 from 0)

有人可以解释这四个部分吗?

最佳答案

你似乎认为:

 a = test[i];

将字符串复制到a 指向的内存中。但实际上它只是丢弃了存储在a 中的指针,并将其替换为指向test 的指针。所以你已经丢失(泄漏)了你分配的内存,因此 freeing 它是一个错误。

关于c - 免费 : Invalid Size : Valgrind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57441834/

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