gpt4 book ai didi

c++ - Valgrind 中的内存泄漏和错误

转载 作者:行者123 更新时间:2023-11-30 02:48:54 25 4
gpt4 key购买 nike

我是 C++ 的初学者,如果我正确地释放内存并删除可能的悬挂指针,我仍然很困惑。这是我过去的学校作业之一。有这么多学生有同样的问题,没有人能帮助我。请指出我哪里有问题。

    ==25334== Mismatched free() / delete / delete []
==25334== at 0x4006D21: free (vg_replace_malloc.c:446)
==25334== by 0x80492F2: HashTable::~HashTable() (Hash.c:115)
==25334== by 0x8049145: SymTab::~SymTab() (SymTab.h:9)
==25334== by 0x8048E9D: main (Driver.c:170)
==25334== Address 0x402c0b8 is 0 bytes inside a block of size 12 alloc'd
==25334== at 0x4007862: operator new(unsigned int) (vg_replace_malloc.c:292)
==25334== by 0x8048C73: main (Driver.c:143)
==25334==
==25334==
==25334== HEAP SUMMARY:
==25334== in use at exit: 18 bytes in 4 blocks
==25334== total heap usage: 10 allocs, 6 frees, 106 bytes allocated
==25334==
==25334== 18 bytes in 4 blocks are definitely lost in loss record 1 of 1
==25334== at 0x4007D58: malloc (vg_replace_malloc.c:270)
==25334== by 0x97E96F: strdup (strdup.c:43)
==25334== by 0x8048FDC: UCSDStudent::UCSDStudent(char*, long) (Driver.c:36)
==25334== by 0x8048C92: main (Driver.c:143)
==25334==
==25334== LEAK SUMMARY:
==25334== definitely lost: 18 bytes in 4 blocks
==25334== indirectly lost: 0 bytes in 0 blocks
==25334== possibly lost: 0 bytes in 0 blocks
==25334== still reachable: 0 bytes in 0 blocks
==25334== suppressed: 0 bytes in 0 blocks
==25334==
==25334== For counts of detected and suppressed errors, rerun with: -v
==25334== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 15 from 8)

基础.h

    #ifndef BASE_H
#define BASE_H

#include <iostream>
using namespace std; /* C error */

/* TEMPLATE */
struct Base { /* C++ struct is public class, public methods */
/* PUBLIC SECTION */
/* virtual: candidates for redefinition */
virtual operator char * (void) {
return 0;
}
virtual operator long (void) { // hash function
return 0;
}
virtual long operator == (Base & base) {// isequal function
return *this == base;
}
Base (void) {} // new_element
virtual ~Base (void) {} // delete_element
virtual ostream & Write (ostream & stream) = 0;// write_element
};

#endif

驱动.c

class UCSDStudent : public Base { /* extends Base  */
char * name;
long studentnum;

public:
UCSDStudent (char * nm, long sn) :
name (strdup (nm)), studentnum (sn) {} /* Initialization */


~UCSDStudent (void) { /* Destructor */
free (name);
}

哈希.c

    /* HashTable constructor */
HashTable :: HashTable (int sz) : size (sz),
table_count(++counter), occupancy (0), table (new Base *[sz]),
probeCount (new int[sz])


HashTable :: ~HashTable (void)
{

/* call function to delete individual elements */
for(int index2 = 0; index2 < size; index2++)
{

if(table[index2] != NULL)
{
free(table[index2]);
table[index2] = NULL;
}

delete table[index2];
}

/*
* delete table itself
* Freed memory
*/
delete[] table;
delete[] probeCount;
/* pointed dangling ptr to NULL */
table = NULL;
probeCount = NULL;
} /* end: ~HashTable */

最佳答案

两个 Valgrind 错误(“Mismatched free()/delete/delete []”和“18 bytes in 4 blocks definitely lost”)可能是相关的。

~HashTable() 中调用 free(table[index2]) 这可能意味着销毁 UCSDStudent 对象(不确定,因为您没有发布整个程序,尤其是没有将元素插入 HashTable 的代码。我假设您使用 new 创建了 UCSDStudent 对象 - 在这种情况下,您还必须使用相应的销毁方法(在本例中为 delete free())。这是第一个 Valgrind 错误的原因。

此外,free() 函数不会调用对象的析构函数,而 delete 会执行此操作。这可以解释为什么 ~UCSDStudent() 没有被调用,导致你的程序泄漏学生姓名的内存。因此,在 ~HashTable() 中使用 delete 而不是 free() 应该可以解决这两个错误。

一般来说,您应该尝试使用一种内存分配方式(malloc()/free()new/new[]/删除/删除[])。鉴于这是一个 C++ 程序,new 将是合适的选择。同样,我建议您删除 strdup()char* 内容并改用 std::string -这将删除另一个您可能混淆 free()delete 的位置。

关于c++ - Valgrind 中的内存泄漏和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749092/

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