我正在使用valgrind來嘗試修復我正在處理的賦值中的許多內存泄漏,並且它給了我幾個不同的讀/寫/未初始化值錯誤。我認爲我知道基於valgrind輸出它來自哪裏,但不知道如何解決它爲我的生活。我對C++很陌生,所以我可能只是做了一些完全不正確的事情,我如何分配(然後試圖訪問錯誤分配的內存)數組的內存,但我無法弄清楚究竟是什麼將會。Valgrind:無效的寫入大小8來自複製構造函數
這裏的各種Valgrind的輸出:
Invalid write of size 8
==13371== at 0x4013F5: family::setFriends(char**) (family.cpp:62)
==13371== by 0x4: 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 0x4: 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)
'朋友' 是在頭文件作爲如此宣佈一個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);
}
我在這裏做錯了什麼來產生所有這些錯誤?
那些valgrind錯誤不描述泄漏。首先關注第一個錯誤 - 是你發佈的第一個錯誤? – aschepler
@aschepler是我的錯誤。我說泄漏,因爲我確實有大量內存泄漏,但我認爲內存泄漏來自我發佈的錯誤。 – ThomasJazz
'addFamily'的代碼在哪裏? – 1201ProgramAlarm