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.
mainfunction? How do you use these functions you show? What do you pass them? What isstack? Acreatefunction that doesn't really create anything? Why do you have your owncopy_stringinstead of the standardstrcpy? And when you catch a crash in a debugger, always check the call stack (using thebtcommand in GDB) so you know where in your code it happens.free(temp);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 bymalloc, you do not create a newstackstructure by that. When you callfree(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.btcommand.stack,2. The function that callspush(). 1 is necessary to understand how the heap is being corrupted. 2 is necessary to understand why your program has an infinite loop.