FINAL_EDIT:發現問題,在for循環計數器,也被用作索引,大於數組元素的數量。關於它的奇怪的事情是,我沒有收到分段錯誤,而是我提到的錯誤。那是爲什麼?malloc.c「sYSMALLOC:斷言」與C++項目執行
謝謝你的幫忙!
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _
我有一個項目在C + +得到這個錯誤(不總是),如果一個特定的全局變量作爲一個大小的int
陣列。
NEW EDIT_1->除了主函數,我還沒有使用new
運算符,我聲明瞭一個指向類的對象的新指針數組。 像這樣:
Student* test[NUM_AM];
for(int i=0; i<NUM_AM; i++)
{
test[i] = new Student(random_elements);
}
有沒有辦法找出導致該行?
的錯誤是這樣(複製GDB的輸出):
malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
&((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &&
old_size == 0) || ((unsigned long) (old_size) >=
(unsigned long)((((__builtin_offsetof (struct malloc_chunk,
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) &&
((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Program received signal SIGABRT, Aborted.
0x00007ffff75563a5 in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
in ../nptl/sysdeps/unix/sysv/linux/raise.c
同樣的valgrind給出了這樣的:
==6775== HEAP SUMMARY:
==6775== in use at exit: 560 bytes in 35 blocks
==6775== total heap usage: 35 allocs, 0 frees, 560 bytes allocated
==6775==
==6775== LEAK SUMMARY:
==6775== definitely lost: 560 bytes in 35 blocks
==6775== indirectly lost: 0 bytes in 0 blocks
==6775== possibly lost: 0 bytes in 0 blocks
==6775== still reachable: 0 bytes in 0 blocks
==6775== suppressed: 0 bytes in 0 blocks
==6775== Rerun with --leak-check=full to see details of leaked memory
==6775==
==6775== For counts of detected and suppressed errors, rerun with: -v
==6775== Use --track-origins=yes to see where uninitialised values come from
==6775== ERROR SUMMARY: 806 errors from 2 contexts (suppressed: 4 from 4)
謝謝您的時間。
NEW_EDIT2: 我跑與應用:
valgrind --leak-check=full --track-origins=yes ./out
and NUM_AM = 25;
這是我得到的輸出是在這裏引擎收錄: here!
NEW_EDIT3: 我應該指出,該程序創建帶有ID的學生,並在布爾數組和2個bool變量中爲其分配真/假值。 變量NUM_AM用作聲明爲類成員的靜態數組的索引。 此外,NUM_AM在搜索其ID時用於多種功能。
在NUM_AM> 22的情況下,即使在創建第一個學生後,也可能發生錯誤。 使用NUM_AM < = 21,我無法通過多次串行執行程序來重現錯誤。
使用'--leak-check = full'和'track-origins = yes' ... – 2012-01-12 19:08:48
它是不是告訴你那些806錯誤和2個上下文是什麼? – 2012-01-12 19:12:19
@KerrekSB:我已經更新了valgrind的輸出作爲額外的信息。 – Chris 2012-01-12 19:47:32