0

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 -

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.

17
  • 3
    When creating a Minimal, Complete, and Verifiable Example, it's important to not forget the complete part. Where's the main function? How do you use these functions you show? What do you pass them? What is stack? A create function that doesn't really create anything? Why do you have your own copy_string instead of the standard strcpy? And when you catch a crash in a debugger, always check the call stack (using the bt command in GDB) so you know where in your code it happens. Commented Jun 19, 2017 at 8:05
  • 2
    Remove free(temp); Commented Jun 19, 2017 at 8:06
  • 2
    The problem with free(temp) is that you free the memory you just allocated for a node you added to your stack. When you link the node into the list, you only link to the memory allocated by malloc, you do not create a new stack structure by that. When you call free(temp) you leave the list with a stray pointer to unallocated memory, leading to undefined behavior when you attempt to access that memory. If this is the reason for the crash we can only speculate on though. Commented Jun 19, 2017 at 8:14
  • 2
    And again, please learn how to use GDB! How to see the function call stack with the bt command. Commented Jun 19, 2017 at 8:17
  • 2
    To answer this question fully, we need: 1. the definition of stack,2. The function that calls push(). 1 is necessary to understand how the heap is being corrupted. 2 is necessary to understand why your program has an infinite loop. Commented Jun 19, 2017 at 8:58

1 Answer 1

1

You are free()ing then accessing the same object when *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.

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:

    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 = temp;
    temp->next = NULL;
Sign up to request clarification or add additional context in comments.

4 Comments

This is not the whole answer. Remember that the crash is happening in free(). I think something else is corrupting the heap.
@JeremyP I won't be surprised if it's not the only error. There's a number of issues with this code but that is certainly a fatal problem. They've also assigned the object they free() to *head which will cause even more trouble later including a possible heap corruption. The code fragment isn't sufficient to actual 'fix' this program.
Please see the comments above, the issue has been resolved
@Masquerade The string terminator? The problems with push() are still fatal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.