Reputation: 37
I am trying to delete the first node in my linked list and I'm not sure how to go about it. My linked list looks like this
typedef struct availableForRent{
int milage;
char plateNum[8];
struct availableForRent * next;
} availablreForRent;
The initial node of my linked list is hard coded exactly like this
struct availableForRent * head = NULL;
head = malloc(sizeof(struct availableForRent));
head->milage = 190000;
fillString(head->plateNum);
head->next = NULL;
fillString just gets a user input and places it in an array
I add a member to my list through a push function like this
void pushAvailable(struct availableForRent * head) {
struct availableForRent * current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(struct availableForRent));
printf("Enter a milage amount: ");
scanf("%d", ¤t->next->milage);
fillString(current->next->plateNum);
current->next->next = NULL;
}
and my function to remove the first member looks like this
struct availableForRent * next_node = *head;
if (next_node->next == NULL) {
printf("Cannot remove member as it is the only data in the list!\n");
return;
}
next_node = next_node->next;
free(*head);
*head = next_node;
When I run my program I get this error message,
rentalQ1(2799,0x7fff9d09b380) malloc: *** error for object 0x7ffee8f62a08: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Does this mean that the node I am trying to free doesn't exist or something else?
Upvotes: 0
Views: 63
Reputation: 37
It appears that not mallocing the next_node was my problem.
next_node = malloc(sizeof(struct availableForRent));
Upvotes: 0