Reputation: 13
I have been experimenting using simple linked lists in C however I am getting the wrong outputs followed by a segmentation fault error.
The purpose of this program is two get 3 parameters from a text file and place them within a linked list as an object. Each time a new line is read it will be placed within the list using the method newData
and will eventually be displayed to the console using the display
method.
However when I try to do this I only output the final item added to the linked list and it is displayed the amount of lines present within my text file which is followed by a segmentation fault error and I cannot fathom why.
If anyone knows the issue or how to fix it would be greatly appreciated. The code is shown below.
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "param.h"
#include "function.h"
#include "lists.h"
int main(int argc, char *argv[])
{
char *p1;
int p2;
int p3;
FILE *in;
char *temp;
char param[100];
in = fopen(argv[1],"r");
while (fgets(param,100,in) != NULL) {
temp = strdup(param);
p1 = strsep(&temp,",");
p2 = atoi(strsep(&temp,","));
p3 = atoi(strsep(&temp,","));
newData(p1,p2,p3);
free(temp);
}
fclose(in);
display();
return 0;
}
lists.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "param.h"
#include "function.h"
#include "lists.h"
static struct node*head;
static struct parameters tempTaskObject;
static int status = 1;
void newData(char * p1, int p2, int p3){
if (status == 1)
{
head = (struct node*)malloc(sizeof(struct node));
}
tempTaskObject.p1 = p1;
tempTaskObject.p2 = p2;
tempTaskObject.p3 = p3;
insert(&head,&tempTaskObject);
status = 0;
}
void display(){
traverse(head);
}
function.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "function.h"
#include "param.h"
void insert(struct node **head, Parameters *newParameters) {
struct node *newNode = malloc(sizeof(struct node));
newNode->parameters = newParameters;
newNode->next = *head;
*head = newNode;
}
void traverse(struct node *head) {
struct node *temp;
temp = head;
while (temp != NULL) {
printf("[%s] [%d] [%d]\n",temp->parameters->p1, temp->parameters->p2, temp->parameters->p3);
temp = temp->next;
}
}
param.h:
#ifndef TASK_H
#define TASK_H
typedef struct parameters {
char *p1;
int p2;
int p3;
} Parameters;
#endif
lists.h:
#define MIN_PRIORITY 1
#define MAX_PRIORITY 10
void newData(char *p1, int p2, int p3);
void display();
function.h:
#include "param.h"
struct node {
Parameters * parameters;
struct node *next;
};
void insert(struct node **head, Parameters * parameters);
void traverse(struct node *head);
[Test Input 1]:
P1, 1, 11
P2, 2, 22
P3, 3, 33
P4, 4, 44
P5, 5, 55
P6, 6, 66
P7, 7, 77
P8, 8, 88
[Test Output 1]:
P8, 8, 88
P8, 8, 88
P8, 8, 88
P8, 8, 88
P8, 8, 88
P8, 8, 88
P8, 8, 88
P8, 8, 88
Segmentation fault (core dumped)
[Test Input 2]:
P1, 1, 11
P2, 2, 22
P3, 3, 33
[Test Output 2]:
P3, 3, 33
P3, 3, 33
P3, 3, 33
Segmentation fault (core dumped)
Upvotes: 1
Views: 54
Reputation: 311038
For starters in this while loop
while (fgets(param,100,in) != NULL) {
temp = strdup(param);
p1 = strsep(&temp,",");
p2 = atoi(strsep(&temp,","));
p3 = atoi(strsep(&temp,","));
newData(p1,p2,p3);
free(temp);
}
the call of free
free(temp);
does not make an effect because temp will be equal to NULL.
On the other hand, the function newData
also can invoke undefined behavior because it uses uninitialized object of the type struct node
when it is called the first time passing it to the function insert.
if (status == 1)
{
head = (struct node*)malloc(sizeof(struct node));
}
Also you are always using a pointer to the same global object static struct parameters tempTaskObject;
in this initializing statement
newNode->parameters = newParameters;
Pay attention to that it is a bad idea to define functions such a way when they depend on numerous global variables.
Upvotes: 1