2011-11-18 70 views
1

我已經調試了幾個小時,我真的完全失去了。幫幫我! 注意:在程序中有比這更多的代碼,但其餘的工作正常,所以我試圖拉所有相關的代碼。如果您發現有任何遺漏,請告訴我。C:Seg故障讓我瘋狂

typedef struct cellT { 
queueElementT value; 
struct cellT *link; 
} cellT; 

struct queueCDT { 
cellT *head; 
cellT *tail; 
}; 

void ReverseQueue(queueADT queue){ 
int i, x, length; 
length = QueueLength(queue); 
cellT *beg, *end; 
queueElementT temp; 

beg = queue->head; 

for(i = 0; i < (length/2); i++){ 
    end = beg; 
    for(x = 0; x < (length-i); x++) 
     end = end->link; 

    /* POINTERS REMAIN, VALUES SWAPPED */ 
    temp = beg->value; 
    beg->value = end->value; /* gdb says issue happens here */ 
    end->value = temp; 
} 
} 
+2

如果你在linux下開發,試試[valgrind](http://stackoverflow.com/questions/7 316306/C-分割故障與 - 的strcmp/7316492#7316492) –

回答

1

的問題可能是這個循環:

for(x = 0; x < (length-i); x++) 

嘗試將其更改爲:

for(x = 0; x < (length-i) && end->link != NULL; x++) 
0

僅僅只掉,如果有東西來交換:

if (beg && end) { 
    temp = beg->value; 
    beg->value = end->value; 
    end->value = temp; 
}