2014-02-11 66 views
1

嘗試釋放列表後,我在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; 
    } 

的重要部分電影類是不是真的那麼特別是如果需要,我可以將它張貼

回答

0

size最初爲0,分配的緩衝區爲0字節。當添加第一部電影時,您太早增加size。它等於1,並在for循環中讀取array[0]

順便說一句,您的調整策略是致命的低效率......考慮根據需要加倍數組的大小。

+1

感謝我的工作了,哦,不要擔心它的任務的一部分,我需要做一定的方式 – user3296933

+0

你的歡迎。考慮加註和/或接受你認爲更有用的答案。堆棧溢出基於聲譽系統。 – gd1

2

功能addMovie無效。

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; 
} 

您增加了對象大小並嘗試訪問數組中不存在的索引大小爲1的數組元素。循環必須爲

for (int x = 0; x < size - 1; x++) { 
    temp[x] = array[x]; 
    } 
相關問題