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

时间:2017-08-06 00:46:19

标签: c++ arrays memory char valgrind

我正在使用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);
}

的getID:

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

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

2 个答案:

答案 0 :(得分:1)

您的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];
    }
}

您应该撤销对NULLnew[]表达式的检查。此外,使用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);
}

答案 1 :(得分:0)

无效写入来自setFriends,您删除分配给friends的内存然后写入内存。在复制好朋友之前,您需要在setFriends中进行新的分配。

未初始化的值消息是因为ID构造函数中有两个名为family的变量:参数和类成员。当你调用setID(ID)时,这是引用类成员(值为NULL),不是参数ID。将参数重命名为与类中的字段具有不同的名称。