已經編輯你的問題有,實際上可以被編譯的代碼,我從Valgrind的幾份報告。以第一種:
==6801== Invalid write of size 4
==6801== at 0x108D49: dynamic_add_last(int*, int, int) (15111518.cpp:19)
==6801== by 0x108F47: main (15111518.cpp:57)
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4)
==6801== by 0x108E6D: main (15111518.cpp:51)
很顯然,你已經分配n
整數,但現在正試圖訪問它們的n+1
:
for (int *i = mass; i < mass + n + 1; i++)
類似的其他報告的錯誤。您的環境可能提供了一個在數組被刪除時進行檢查的金絲雀,這就是您的報告引用該行的原因。您需要仔細查看代碼的其餘部分,以查看您寫入的範圍。作爲參考,這裏的Valgrind的錯誤,我輸入了2 4 3 3
:
==6801== Memcheck, a memory error detector
==6801== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6801== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==6801== Command: ./15111518
==6801==
Massive size: 0 - 9
1 - 2
Number what you want to add: ==6801== Invalid write of size 4
==6801== at 0x108D49: dynamic_add_last(int*, int, int) (15111518.cpp:19)
==6801== by 0x108F47: main (15111518.cpp:57)
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4)
==6801== by 0x108E6D: main (15111518.cpp:51)
==6801==
0 - 9
1 - 2
==6801== Invalid read of size 4
==6801== at 0x108F77: main (15111518.cpp:60)
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4)
==6801== by 0x108E6D: main (15111518.cpp:51)
==6801==
2 - 4
Position: Number what you want to add: ==6801== Invalid read of size 4
==6801== at 0x108DF1: dynamic_add(int*, int, int, int) (15111518.cpp:31)
==6801== by 0x10903E: main (15111518.cpp:64)
==6801== Address 0x5a85108 is 0 bytes after a block of size 8 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108C71: dynamic(int) (15111518.cpp:4)
==6801== by 0x108E6D: main (15111518.cpp:51)
==6801==
==6801== Invalid write of size 4
==6801== at 0x108DE9: dynamic_add(int*, int, int, int) (15111518.cpp:28)
==6801== by 0x10903E: main (15111518.cpp:64)
==6801== Address 0x5a8515c is 0 bytes after a block of size 12 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108D89: dynamic_add(int*, int, int, int) (15111518.cpp:24)
==6801== by 0x10903E: main (15111518.cpp:64)
==6801==
0 - 9
1 - 2
2 - 4
==6801== Invalid read of size 4
==6801== at 0x109072: main (15111518.cpp:67)
==6801== Address 0x5a8515c is 0 bytes after a block of size 12 alloc'd
==6801== at 0x4C2B93F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6801== by 0x108D89: dynamic_add(int*, int, int, int) (15111518.cpp:24)
==6801== by 0x10903E: main (15111518.cpp:64)
==6801==
3 - 3
==6801==
==6801== HEAP SUMMARY:
==6801== in use at exit: 12 bytes in 1 blocks
==6801== total heap usage: 5 allocs, 4 frees, 77,844 bytes allocated
==6801==
==6801== LEAK SUMMARY:
==6801== definitely lost: 12 bytes in 1 blocks
==6801== indirectly lost: 0 bytes in 0 blocks
==6801== possibly lost: 0 bytes in 0 blocks
==6801== still reachable: 0 bytes in 0 blocks
==6801== suppressed: 0 bytes in 0 blocks
==6801== Rerun with --leak-check=full to see details of leaked memory
==6801==
==6801== For counts of detected and suppressed errors, rerun with: -v
==6801== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
你'刪除[] mass'是在示例代碼中未顯示的分配 - 我們可以不知道在哪裏的腐敗現象發生的開始與(它不在這個代碼中)。 – Niall
如果你使用std :: vector,你會在訪問一個無效索引時從調試器中獲得一個通知。 –
i