I am trying to implement linked-list in C. I used a structure called NODE. For some reason, I get a Segmentation Error 11
when I call the function createList()
. I debugged my code and I am pretty confident that the error occurs here:
/* Write into node */
strcpy(head->username, cur_username);
strcpy(head->password, cur_pw);
strcpy(head->type, cur_type);
printf("%s %s %s\n", head->username, head->password, head->type);
I think the error is because memory was not assigned correctly. But I initialized an array of 50 chars for each node value, so shouldn't the program manage the memory by itself?
char *filename = "password.csv";
int n_users = 0;
struct NODE {
char username[50];
char password[50];
char type[50];
struct NODE *next;
} *head=NULL, *curr=NULL;
/* Global placeholders so they can be used by other both add() and creatList() */
char line[50], *cur_username, *cur_pw, *cur_type;
char linecopy[50];
void createList(void) {
printf("Creating list\n");
bool success;
/* Open the file in read-only and copy content into array */
/* File pointer */
FILE *f;
/* Node pointer */
struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE));
if (NULL == ptr) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
head = curr = ptr;
/* Open the file in read */
f = fopen("password.csv", "r");
/* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */
if (f != NULL) {
/* Read the file line by line */
while(fgets(line, 49, f) != NULL) {
struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE));
/* Copy line into linecopy because strtok is destructive */
strcpy(linecopy, line);
/* Extract the ids from linecopy */
cur_username = strtok(linecopy, ",");;
cur_pw = strtok(NULL, ",");
cur_type = strtok(NULL, ",");
if(head==NULL)
add(head);
else
add(p);
if (!success) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
}
}
/* Close file */
fclose(f);
}
** HEAD is NULL **
While I don't have any segmentation fault anymore, my head does not get initialized... I am reading inputs from a file and create a node for each line of the file. Therefore, I am testing for head==NULL and sending head to add(struct *NODE). Shouldn't that initialize head?
struct NODE {
char username[50];
char password[50];
char type[50];
struct NODE *next;
} *head=NULL, *curr=NULL;
/* Global placeholders so they can be used by other both add() and creatList() */
char line[50], *cur_username, *cur_pw, *cur_type;
char linecopy[50];
void createList(void) {
printf("Creating list\n");
bool success;
/* Open the file in read-only and copy content into array */
/* File pointer */
FILE *f;
/* Node pointer */
struct NODE * ptr = (struct NODE*) malloc(sizeof(struct NODE));
if (NULL == ptr) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
head = curr = ptr;
/* Open the file in read */
f = fopen("password.csv", "r");
/* if f == NULL, there was an error - the file probably does not exist. We don't care, we will create the file later */
if (f != NULL) {
/* Read the file line by line */
while(fgets(line, 49, f) != NULL) {
struct NODE * p = (struct NODE*) malloc(sizeof(struct NODE));
/* Copy line into linecopy because strtok is destructive */
strcpy(linecopy, line);
/* Extract the ids from linecopy */
cur_username = strtok(linecopy, ",");;
cur_pw = strtok(NULL, ",");
cur_type = strtok(NULL, ",");
if (head == NULL)
success = add(head);
else
success = add(p);
if (!success) {
printf("\nError: was unable to initialize password validation information!! \n");
return;
}
}
}
/* Close file */
fclose(f);
}
Yes, this would be a problem.
/* Is list empty? */
if (head == NULL) {
/* Write into node */
strcpy(head->username, cur_username);
strcpy(head->password, cur_pw);
strcpy(head->type, cur_type);
printf("%s %s %s\n", head->username, head->password, head->type);
}
Dereferencing NULL
will generally cause a segmentation fault.
In this case, you always want to allocate a new node. Try this:
struct NODE *node = malloc(sizeof(struct NODE));
if (node != NULL)
{
strcpy(node->username, cur_username);
strcpy(node->password, cur_pw);
strcpy(node->type, cur_type);
/* push node onto list head */
node->next = head;
head = node;
}
在我看来,人头没有分配
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.