简体   繁体   English

使用链接列表时malloc中的分段错误

[英]Segmentation fault in malloc while using linked list

The program reads from a file, stores the required data in some variables then push it to a stack. 程序从文件读取,将所需的数据存储在一些变量中,然后将其推入堆栈。 This is a small part of the input file, the file contains this data repeated several times( value of variables changed in each chunk of data ). 这是输入文件的一小部分,该文件包含重复几次的该数据(每个数据块中变量的值都已更改)。

----------------------------------------------------------------------- 
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> 

The stored data is pushed to a stack where the segmentation fault is occurring. 存储的数据被推入发生分段错误的堆栈。 The program is able to store only one stack after that the fault occurs. 发生故障后,该程序只能存储一个堆栈。

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);
}

Using gdb( GNU Debugger ) i got this error message - 使用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.

SOLVED : The reason for the whole problem was free(temp), it needed to be removed from the above code and also in the main file, file pointer was getting closed by mistake after running the code once so infinite loop was running while taking input again. 已解决:整个问题的原因是free(temp),需要将其从上述代码中删除,并且还在主文件中,运行代码一次后文件指针被错误关闭,因此在输入时运行无限循环再次。

You are free() ing then accessing the same object when *head==NULL . 您可以通过free()然后在*head==NULL时访问同一对象。 Look at the lines marked **** below. 查看下面标有****的行。

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);

So remove free(temp) it is in the totally wrong place but you haven't given enough code to say where it goes. 因此,删除free(temp)它放在完全错误的位置,但是您没有给出足够的代码来说明它的去向。

The 'indefinite looping' is some other error but you haven't provided enough code to identify that. “无限循环”是其他一些错误,但是您没有提供足够的代码来识别它。

Also notice the else part doesn't make much sense: 还要注意, else部分没有多大意义:

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

It's not clear what tracker is going in but assuming it is valid this amounts to: 目前尚不清楚tracker要使用什么,但是假设它有效,则等于:

    tracker = temp;
    temp->next = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM