Reputation: 91
I have a struct of a linked list the have a node struct inside
struct gradeNode {
char courseName[sizeName];
int grade;
struct gradeNode *next;
struct gradeNode *prev;
struct gradeList {
struct gradeNode *head;
struct gradeNode *next;
I'm trying to destroy the list free!! but i get exception of access violation any help
void destroyList(struct gradeList *head)
{
struct gradeNode* tmp;
while (head!= NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
free(head);
}
Here is the main
int i = 0;
for (i; i < numOfStuds; i++) {
destroyList(&students[i].gradelist);
}
Upvotes: 0
Views: 211
Reputation: 4604
void destroyList(struct gradeList *head)
{
struct gradeNode* tmp;
while (head!= NULL)
{
tmp = head; //<<<<<<< assigning a struct gradeList* to a struct gradeNode*
head = head->next;
free(tmp); //<<<<<<< freeing the alias
}
You're assigning a pointer of one type to a pointer of another type and then freeing it, which is especially problematic since struct gradeList
appears to be a member of struct gradeNode
.
Check your warnings. Are you not getting a warning for tmp = head
? With gcc 7.2 I get:
[x86-64 gcc 7.2 #1] warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
Upvotes: 1