0
您好我正在運行的合併短,這是造成核心轉儲故障,出現以下錯誤比較功能:歸併排序與比較功能 - 內核傾倒
part1 21 shortbooks.txt
*** glibc detected *** part1: free(): invalid next size (fast): 0x0867b258 ***
======= Backtrace: =========
/lib/libc.so.6[0x409fb6]
part1[0x80489c6]
part1[0x80487fb]
part1[0x80487fb]
part1[0x80487fb]
part1[0x8048ac9]
part1[0x8048d41]
/lib/libc.so.6(__libc_start_main+0xe6)[0x3b1e36]
part1[0x8048561]
======= Memory map: ========
00376000-00393000 r-xp 00000000 fd:00 1311353 /lib/ld-2.13.so
00393000-00394000 r--p 0001c000 fd:00 1311353 /lib/ld-2.13.so
00394000-00395000 rw-p 0001d000 fd:00 1311353 /lib/ld-2.13.so
0039b000-0051e000 r-xp 00000000 fd:00 1311360 /lib/libc-2.13.so
0051e000-0051f000 ---p 00183000 fd:00 1311360 /lib/libc-2.13.so
0051f000-00521000 r--p 00183000 fd:00 1311360 /lib/libc-2.13.so
00521000-00522000 rw-p 00185000 fd:00 1311360 /lib/libc-2.13.so
00522000-00525000 rw-p 00000000 00:00 0
00577000-00593000 r-xp 00000000 fd:00 1345987 /lib/libgcc_s-4.5.1-20100924.so.1
00593000-00594000 rw-p 0001b000 fd:00 1345987 /lib/libgcc_s-4.5.1-20100924.so.1
008e6000-008e7000 r-xp 00000000 00:00 0 [vdso]
08048000-08049000 r-xp 00000000 00:19 435651043 /home/S10/rowlanj0/COMP26120/ex7/part1
08049000-0804a000 rw-p 00001000 00:19 435651043 /home/S10/rowlanj0/COMP26120/ex7/part1
0867b000-0869c000 rw-p 00000000 00:00 0 [heap]
b783b000-b783c000 rw-p 00000000 00:00 0
b7867000-b7868000 rw-p 00000000 00:00 0
bfb25000-bfb49000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
是任何人都可以提供建議可能是什麼造成這種故障以及如何解決它?
void memcopy(B *to, B *from, int count) {
while (count-- > 0) *to++ = *from++ ;
}
void merge_sort(B* book, int n, int elementsize, int (*p_cmp_f)()) {
B *firsthalf, *endoffirsthalf, *secondhalf, *endofsecondhalf, *resultbuffer, *p_result;
int halfsize ;
if (n <= 1) return ;
halfsize = n/2 ;
firsthalf = book ;
secondhalf = book + halfsize * elementsize ;
merge_sort(firsthalf, halfsize, elementsize, p_cmp_f) ;
merge_sort(secondhalf, n - halfsize, elementsize, p_cmp_f) ;
endoffirsthalf = secondhalf ;
endofsecondhalf = book + n * elementsize ;
resultbuffer = (B*) malloc(n * elementsize) ;
p_result = resultbuffer ;
while(firsthalf < endoffirsthalf && secondhalf < endofsecondhalf) {
if((*p_cmp_f)(firsthalf, secondhalf) < 0) {
memcopy(p_result, firsthalf, elementsize) ;
firsthalf += elementsize ;
}
else {
memcopy(p_result, secondhalf, elementsize) ;
secondhalf += elementsize ;
}
p_result += elementsize ;
}
while(firsthalf < endoffirsthalf) {
memcopy(p_result, firsthalf, elementsize) ;
firsthalf += elementsize ;
p_result += elementsize ;
}
while(secondhalf < endofsecondhalf) {
memcopy(p_result, secondhalf, elementsize) ;
secondhalf += elementsize ;
p_result += elementsize ;
}
memcopy(book, resultbuffer, n * elementsize) ;
free(resultbuffer) ;
}
這裏是要求的代碼。
這裏的結構和比較功能之一:
typedef struct book{
double rating;
double price;
double relevance;
int ID;
}B;
和比較功能:
int comp_on_price(const void *a, const void *b)
{
if ((*(B *)a).price < (*(B *)b).price)
return 1;
else if ((*(B *)a).price > (*(B *)b).price)
return -1;
else
return 0;
}
在我看來,你試圖'free()'你沒有'malloc()'的指針。一些代碼會有所幫助。 – drdwilcox
如果使用-g進行編譯並從gdb運行應用程序,並在崩潰後鍵入「backtrace」,則可以獲得更多信息 – perreal
如果您沒有發佈代碼,我們應該如何提供幫助? – pajton