gpt4 book ai didi

c++ - Valgrind:来自复制构造函数的大小为 8 的无效写入

转载 作者:行者123 更新时间:2023-11-30 05:10:43 24 4
gpt4 key购买 nike

我正在使用 valgrind 尝试修复我正在处理的任务中的许多内存泄漏,它给我一些不同的读/写/未初始化值错误。我认为我根据 valgrind 输出知道它来自哪里,但无法弄清楚如何在我的生活中修复它。我是 C++ 的新手,所以我可能只是做了一些完全不正确的事情,我如何分配(然后尝试访问错误分配的内存)数组的内存,但我无法弄清楚那到底是什么会是。

这是各种 valgrind 输出:

Invalid write of size 8
==13371== at 0x4013F5: family::setFriends(char**) (family.cpp:62)
==13371== by 0x401231: family::family(family const&) (family.cpp:31)
==13371== by 0x402358: hashtable::node::node(family const&) (hashtable.h:29)
==13371== by 0x401E81: hashtable::insert(char const*, family const&) (hashtable.cpp:87)
==13371== by 0x4018CD: familymgr::addFamily(family&) (familymgr.cpp:15)
==13371== by 0x402779: main (housinghelper.cpp:86)
==13371== Address 0x5ad1810 is 0 bytes inside a block of size 32 free'd
==13371== at 0x4C2F650: operator delete[](void*) (vg_replace_malloc.c:621)
==13371== by 0x4013B5: family::setFriends(char**) (family.cpp:60)
==13371== by 0x401231: family::family(family const&) (family.cpp:31)
==13371== by 0x402358: hashtable::node::node(family const&) (hashtable.h:29)
==13371== by 0x401E81: hashtable::insert(char const*, family const&) (hashtable.cpp:87)
==13371== by 0x4018CD: familymgr::addFamily(family&) (familymgr.cpp:15)
==13371== by 0x402779: main (housinghelper.cpp:86)
==13371== Block was alloc'd at
==13371== at 0x4C2E8BB: operator new[](unsigned long) (vg_replace_malloc.c:423)
==13371== by 0x40120F: family::family(family const&) (family.cpp:29)
==13371== by 0x402358: hashtable::node::node(family const&) (hashtable.h:29)
==13371== by 0x401E81: hashtable::insert(char const*, family const&) (hashtable.cpp:87)
==13371== by 0x4018CD: familymgr::addFamily(family&) (familymgr.cpp:15)
==13371== by 0x402779: main (housinghelper.cpp:86)

未初始化值消息:

Use of uninitialised value of size 8
==13371== at 0x401E98: hashtable::insert(char const*, family const&) (hashtable.cpp:90)
==13371== by 0x4018CD: familymgr::addFamily(family&) (familymgr.cpp:15)
==13371== by 0x402779: main (housinghelper.cpp:86)
==13371== Uninitialised value was created by a stack allocation
==13371== at 0x401882: familymgr::addFamily(family&) (familymgr.cpp:11)
==13371==
==13371== Use of uninitialised value of size 8
==13371== at 0x401EB9: hashtable::insert(char const*, family const&) (hashtable.cpp:91)
==13371== by 0x4018CD: familymgr::addFamily(family&) (familymgr.cpp:15)
==13371== by 0x402779: main (housinghelper.cpp:86)
==13371== Uninitialised value was created by a stack allocation
==13371== at 0x401882: familymgr::addFamily(family&) (familymgr.cpp:11)

'friends'是一个char**成员变量,在头文件中声明如下:

char **friends;

一切似乎都源于复制构造函数:

family::family(const family &fam) : ID(NULL), famName(NULL), 
friends(NULL){
setID(fam.ID);
setFamName(fam.famName);
setMembers(fam.members);
setCapacity(fam.capacity);
setNumFriends(fam.numFriends);

setFriends(fam.friends);
}

这是主要构造函数,以防万一我没有从一开始就为 friends 正确分配内存:

    family::family(char *ID, char *famName, int members) :
ID(NULL),
famName(NULL),
members(members),
numFriends(-1),
capacity(DEFAULT_CAPACITY)
{
friends = new char*[capacity];
for (int i = 0; i < numFriends; i++)
friends[i] = NULL;

setID(ID);
setFamName(famName);
}

这里是被引用的 setFriends 函数:

void family::setFriends(char** friendIn){
friends = new char*[sizeof(friendIn[0])/numFriends];
if(friends!=NULL)
delete [] friends;
for (int i = 0; i < capacity;i++){
this->friends[i] = friendIn[i];
}
}

bool familymgr::addFamily(family &inputFam) {
char fam[100];
inputFam.getID(fam);

table->insert(fam, inputFam);
}

获取ID:

void family::getID(char *id) const {
strcpy(id, this->ID);
}

我在这里做错了什么会产生所有这些错误?

最佳答案

你的 setFriends方法写入您不拥有的内存:

void family::setFriends(char** friendIn){
friends = new char*[sizeof(friendIn[0])/numFriends];
if(friends!=NULL) // <-- This will always be true, since friends was
// assigned a non-null value in the new[]
// expression above
delete [] friends; // <-- Here you free the memory you just allocated
for (int i = 0; i < capacity;i++){
// At this point, friends is no longer pointing to a valid object
// so you are trying to write to memory you don't own
this->friends[i] = friendIn[i];
}
}

您应该撤销对 NULL 的检查和 new[]表达。此外,使用 sizeof(friendIn[0])/numFriends没有意义。 sizeof(friendIn[0]将始终为 4 或 8,具体取决于您的平台的位数。我猜应该是 capacity :

void family::setFriends(char** friendIn){
if(friends != nullptr) {
delete [] friends;
}
friends = new char*[capacity];
for (int i = 0; i < capacity;i++){
this->friends[i] = friendIn[i];
}
}

这在从复制构造函数调用时会起作用,但请记住,您仍在制作 friends 的浅表拷贝。 .两者 family对象将指向相同的字符串,如果您稍后 delete[]那些字符串,那么任何仍然指向它们的对象都将不起作用。你真的应该做 friends一个std::vector<std::string>>而不是执行所有这些手动内存管理。然后你可以制作setFriends更简单也更安全:

void family::setFriends(std::vector<std::string>> friendIn) {
friends = std::move(friendIn);
}

关于c++ - Valgrind:来自复制构造函数的大小为 8 的无效写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527621/

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