簡體   English   中英

使用鏈接列表時malloc中的分段錯誤

[英]Segmentation fault in malloc while using linked list

程序從文件讀取,將所需的數據存儲在一些變量中,然后將其推入堆棧。 這是輸入文件的一小部分,該文件包含重復幾次的該數據(每個數據塊中變量的值都已更改)。

----------------------------------------------------------------------- 
Timestamp (Wed Mar 29 20:44:08 2017) 
[1] Received msg from node <00116 / fc:c2:3d:00:00:10:ab:35> 
RSSI -6 dBm / LQI 22
+[Node_Voltage]  <2.963000 Volts> 
+[P_MS5637]      <896 mbar> 
+[NTC_THERM (Murata NXFT15XH103)]    <27.755314 deg C> 
+[Temp_LM75B]    <27.620001 Deg C> 
+[RH_CC2D33S]    <33.000000 %> 
+[Temp_CC2D33S]  <27.000000 Deg C> 

存儲的數據被推入發生分段錯誤的堆棧。 發生故障后,該程序只能存儲一個堆棧。

void create(stack **head){
    *head=NULL;
}

void copy_string(char arr[],char arr2[]){
    int i=0;

    for(i=0;i<strlen(arr2);i++){
        arr[i] = arr2[i];
    }
}

stack demo;          
stack *tracker;

// **head is used since this is an ADT, i've not pasted the code in source file here 

void push(stack **head,char date[],char time[],char month[],char year[],float pressure,float temprature1,float temprature2,float rel_humid,float node_voltage){
    stack *temp = malloc(sizeof(demo));   

    temp->pressure = pressure;
    temp->temprature1 = temprature1;
    temp->temprature2 = temprature2;
    temp->rel_humid = rel_humid;
    temp->node_voltage = node_voltage;

    printf("Inside push function\n");

    copy_string(temp->date, date);
    copy_string(temp->time, time);
    copy_string(temp->month, month);
    copy_string(temp->year, year);


    if(*head==NULL){
        temp->next = NULL;
        *head = temp;
        tracker = temp;
    }
    else{
        tracker->next = temp;
        tracker = tracker->next;
        tracker->next = NULL;
    }

    free(temp);     //on removing this, program runs infinitely instead of giving segmentation fault

    printf("%s %s %f %f ",tracker->date,tracker->year,tracker->pressure,tracker->node_voltage);
}

使用gdb(GNU Debugger)我收到此錯誤消息-

Inside push function

29 2017) 896.000000 2.963000 Done!!

Program received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x11f1) at malloc.c:2949
2949    malloc.c: No such file or directory.

已解決:整個問題的原因是free(temp),需要將其從上述代碼中刪除,並且還在主文件中,運行代碼一次后文件指針被錯誤關閉,因此在輸入時運行無限循環再次。

您可以通過free()然后在*head==NULL時訪問同一對象。 查看下面標有****的行。

if(*head==NULL){
    temp->next = NULL;
    *head = temp;
    tracker = temp;//**** temp is assigned to tracker. They point to the same place.
}
else{
    //...
}

free(temp);     //**** temp is free()d but remember tracker==temp..

//**** Now you output tracker but the object at this location was just freed.
printf("%s %s %f %f ",tracker->date,tracker->year,tracker->pressure,tracker->node_voltage);

因此,刪除free(temp)它放在完全錯誤的位置,但是您沒有給出足夠的代碼來說明它的去向。

“無限循環”是其他一些錯誤,但是您沒有提供足夠的代碼來識別它。

還要注意, else部分沒有多大意義:

    tracker->next = temp;
    tracker = tracker->next;
    tracker->next = NULL;

目前尚不清楚tracker要使用什么,但是假設它有效,則等於:

    tracker = temp;
    temp->next = NULL;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM