嘗試釋放列表後,我在Valgrind中出現此錯誤。Valgrind無效的讀取大小爲4的錯誤?
這是Valgrind的的重要組成部分
==12349== Invalid read of size 4
==12349== at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
==12349== by 0x8048D11: main (in /home/admin/a2/mbd)
==12349== Address 0x60 is 0 bytes after a block of size 0 alloc'd
==12349== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==12349== by 0x8048B62: DynArray::DynArray() (in /home/admin/a2/mbd)
==12349== by 0x8048C87: main (in /home/admin/a2/mbd)
==12349==
==12349== Invalid read of size 4
==12349== at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
==12349== by 0x8048D97: main (in /home/admin/a2/mbd)
==12349== Address 0x4321114 is 0 bytes after a block of size 4 alloc'd
==12349== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==12349== by 0x8048B90: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
==12349== by 0x8048D11: main (in /home/admin/a2/mbd)
==12349==
==12349== Invalid read of size 4
==12349== at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
==12349== by 0x8048E1D: main (in /home/admin/a2/mbd)
==12349== Address 0x43211d0 is 0 bytes after a block of size 8 alloc'd
==12349== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==12349== by 0x8048B90: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
==12349== by 0x8048D97: main (in /home/admin/a2/mbd)
DynArray.cc
DynArray::DynArray() {
size = 0;
array = new Movie *[size];
}
void DynArray::addMovie(Movie* m) {
size++;
Movie **temp = new Movie *[size];
for (int x = 0; x < size; x++) {
temp[x] = array[x];
}
delete [] array;
temp[size-1] = m;
array = temp;
}
void DynArray::cleanup() {
for (int x = 0; x < size; x++)
delete array[x];
delete [] array;
}
int main() {
DynArray *a = new DynArray();
Movie *m = new Movie("sa", 2129, C_COMEDY);
a->addMovie(m);
Movie *k = new Movie("sas", 4324, C_DRAMA);
a->addMovie(k);
Movie *l = new Movie("dsad", 43241, C_DRAMA);
a->addMovie(l);
for (int x = 0; x < a->size; x++) {
a->array[x]->printMovie();
}
a->cleanup();
delete a;
return 0;
}
的重要部分電影類是不是真的那麼特別是如果需要,我可以將它張貼
感謝我的工作了,哦,不要擔心它的任務的一部分,我需要做一定的方式 – user3296933
你的歡迎。考慮加註和/或接受你認爲更有用的答案。堆棧溢出基於聲譽系統。 – gd1