[英]SegFault on return statement C
我在這段代碼中遇到的問題是,當我嘗試在第一次迭代中返回addTrash函數中的struct node * head時。 我懷疑這可能是堆棧損壞,但我不確定,我也不知道我如何確定我給您的代碼中的錯誤。
這是一個雙向鏈接的鏈表,其值僅作為其持有的數據。
struct node * modifyMainList( struct node *head, int link2Delete){
struct node * curr = head;
struct node * temp;
int i = 0;
//traverse list link2Delete amount of times
while(i != link2Delete){
curr = curr -> next;
}
//head case
if(curr -> previous == NULL){
curr = curr -> next;
head = curr;
return head;
}
//tail case
if(curr -> next == NULL){
temp = curr;
curr = curr -> previous;
curr -> next = NULL;
temp -> previous = NULL;
temp -> next = NULL;
free(temp);
return head;
}
curr -> previous -> next = curr -> next;
curr -> next -> previous = curr -> previous;
curr -> previous = NULL;
curr -> next = NULL;
free(curr);
return head;
}
struct node * addTrash(struct node *mainHead, int link2Delete){
struct node * head = NULL;
struct node * curr = mainHead;
struct node * trashCurr = NULL;;
struct node * temp = NULL;
int i = 0;
printf("im in trash before loop\n\n");
for(i = 0; i < link2Delete; i++){
curr = curr -> next;
}
printf("im in trash before head size check\n\n");
if(head == NULL){
printf("im in trash with head == null\n\n");
//head of main list
if(link2Delete == 0){
printf("im in trash link2delete == null\n\n");
curr = curr -> previous;
head = curr;
curr = curr -> next;
curr -> previous = NULL;
curr -> next = NULL;
return head;
}
printf("im in trash before tail case\n\n");
//tail of main list
if(curr -> next == NULL){
printf("im in trash with tail case\n\n");
head = curr;
head -> previous = NULL;
return head;
}
printf("im in trash before after tail case\n\n");
//every other case
//printf("this is the head value: %d\n\n", head -> value);
head = curr;
//printf("im in trash after head = curr\n\n");
head -> previous = NULL;
//printf("im in trash after head -> previous\n\n");
head -> next = NULL;
printf("im in trash after head -> next\n\n");
printf("this is the head value: %d\n\n", head -> value);
return head;
}else{
printf("im in trash inside else\n\n");
trashCurr = head;
while(trashCurr -> next != NULL){
trashCurr = trashCurr -> next;
}
if(link2Delete == 0){
temp = curr;
trashCurr -> next = temp;
temp -> previous = trashCurr;
trashCurr = temp;
trashCurr -> next = NULL;
return head;
}
//tail of main list
if(curr -> next == NULL){
temp = curr;
trashCurr = temp;
temp -> previous = trashCurr;
temp -> next = NULL;
trashCurr -> next = temp;
return head;
}
//every other case
temp = curr;
temp -> previous = trashCurr;
trashCurr -> next = temp;
trashCurr = temp;
trashCurr -> next = NULL;
return head;
}
}
void generateRandom(struct node *mainHead, int size){
int i = 0;
int link2Delete = 0;
struct node *head = NULL;
srand ( time(NULL) );
int number2Delete = rand() % size + 1;
printf("this is the rand number: %d\n", rand());
printf("this is the number of nodes to be deleted: %d\n", number2Delete);
for (i = 0; i < number2Delete; i++) {
// Pick a random node (payload) to delete.
link2Delete = (rand() % size);
printf("this is the number of nodes in the list: %d\n", size);
printf("this is the node to be deleted: %d\n", link2Delete);
size--;
if(link2Delete == 0){
mainHead = modifyMainList(mainHead, link2Delete);
//printf("this is the call return: %d\n\n", addTrash(mainHead, link2Delete) -> value);
head = addTrash (mainHead, link2Delete);
}else{
head = addTrash (mainHead, link2Delete);
mainHead = modifyMainList(mainHead, link2Delete);
}
}
return;
}
在您的代碼中
while(i != link2Delete){
curr = curr -> next;
}
如果link2Delete!=0
則是一個無限循環。
而且,不是在釋放head case
。
由於循環是無限的,因此curr = curr->next
將繼續重復,並將指向某些垃圾指針(如果循環條件為true)。 然后,它具有不確定的行為。 您可能會得到SEGFAULT。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.