已经编辑你的问题有,实际上可以被编译的代码,我从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