2015-02-08 32 views
0

我已經實現了我自己的簡單的載體,所有的功能似乎除了在alloc_new()很好,當它試圖創建新的內存拷貝的內容他們並刪除舊的內存分配。 我的程序總是掛起,之後永遠不會執行。C++刪除[]總是掛着

   class myvector{ 
      private: 
       int vsize, maxsize; 
       int* arr; 
       void alloc_new(); 
      public: 
       myvector(); 
       myvector(int); 
       myvector(const myvector&); 
       ~myvector(); 

       void push_back(int); 
       int size(); 
       int operator[](int); 
       int at(int); 
       void display(); 
      }; 

      myvector::myvector(){ 
       maxsize = 20; 
       vsize = 0; 
       arr = new int[maxsize]; 
      } 

      void myvector::alloc_new(){ 
       // Allocate new space, double of current size 
       maxsize = maxsize*2; 
       int* arr_new = new int[maxsize]; 
       //copy the elements from the base location to new location 
       for(int i=0; i < vsize ; i++) 
        arr_new[i] = arr[i]; 
       delete[] arr; // MY PROGRAM ALWAYS HANGS HERE 
       arr = arr_new; 
      } 

      void myvector::push_back(int val){ 
       if((vsize+1) > maxsize) 
        alloc_new(); 
       arr[++vsize] = val; 
      } 

      int main(){ 
       myvector vect; 
       for(int i=0;i<25;i++){ 
        vect.push_back(rand()%100+1); 
       } 
       getch(); 
      } 
+1

這通常是你破壞堆的標誌 - 嘗試下的valgrind運行。 – 2015-02-08 08:38:03

+0

實驗表明,這種情況用Visual C++時'maxsize'最初20 - 但不是例如19或21.我懷疑它與底層OS分配器的塊大小有關,並且這是一個編譯器錯誤。您可以在「Microsoft連接」網站上報告它。 – 2015-02-08 08:58:50

+1

@ Cheersandhth. - 阿爾夫:錯誤中的push_back越過邊緣。據推測,這則還需要與分配是「大小均勻」不謀而合,或者你只是簡單地覆蓋在分配備用空間(我的Linux系統上,它運行完全正常,但Valgrind的發現問題)。 – 2015-02-08 09:00:58

回答

2

問題是您的「我們達到了最大尺寸」是錯誤的。你在比較vsize + 1 > maxsize。這意味着,你正在寫arr[++vsize]vsize + 1 == maxsize。這寫入一個元素PAST你的分配[換句話說,索引20是0..19是有效索引],因此出錯。

修復這樣的代碼:

void myvector::push_back(int val){ 
    if((vsize+1) >= maxsize) 
    alloc_new(); 
    arr[++vsize] = val; 
} 

使用的valgrind會告訴你:

$ valgrind ./a.out 
==20918== Memcheck, a memory error detector 
==20918== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 
==20918== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info 
==20918== Command: ./a.out 
==20918== 
==20918== Invalid write of size 4 
==20918== at 0x4009E9: myvector::push_back(int) (in /home/MatsP/src/junk/a.out) 
==20918== by 0x400AA1: main (in /home/MatsP/src/junk/a.out) 
==20918== Address 0x5a20090 is 0 bytes after a block of size 80 alloc'd 
==20918== at 0x4C2A77C: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==20918== by 0x4008B4: myvector::myvector() (in /home/MatsP/src/junk/a.out) 
==20918== by 0x400A57: main (in /home/MatsP/src/junk/a.out) 
==20918== 
==20918== 
==20918== HEAP SUMMARY: 
==20918==  in use at exit: 0 bytes in 0 blocks 
==20918== total heap usage: 2 allocs, 2 frees, 240 bytes allocated 
==20918== 
==20918== All heap blocks were freed -- no leaks are possible 
==20918== 
==20918== For counts of detected and suppressed errors, rerun with: -v 
==20918== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 
+1

**➕1**斑點! [[[[[[[[[[[[ – 2015-02-08 09:04:55

+2

其實,我跑的valgrind ...;) – 2015-02-08 09:07:06

+0

感謝墊皮特森,我改變了的push_back邊界檢查....它爲我工作。還注意到Valgrind工具供將來參考... !!!! :) – 2015-02-08 18:53:07