I have data structure like this:
typedef struct telephoneBookNode
{
int id;
char name[NAME_LENGTH];
char telephone[TELEPHONE_LENGTH];
struct telephoneBookNode * previousNode;
struct telephoneBookNode * nextNode;
} TelephoneBookNode;
typedef struct telephoneBookList
{
TelephoneBookNode * head;
TelephoneBookNode * tail;
TelephoneBookNode * current;
unsigned size;
} TelephoneBookList;
I can create Nodes, and make the List containing data because I got no problems in displaying the List, inserting or moving Nodes....
But when I write function to erase the List, I got error:
PhoneBook(6187,0x7fff77045000) malloc: *** error for object 0x7f87c1f00004: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
This is my eraser functions:
void freeTelephoneBookList(TelephoneBookList* aList) {
TelephoneBookNode* node;
TelephoneBookNode* temp = aList->head;
while (temp) {
node = temp;
temp = node->nextNode;
freeTelephoneBookNode(node);
}
free(aList);
}
void freeTelephoneBookNode(TelephoneBookNode * node) {
free(node->name);
free(node->telephone);
free(node);
}
Please anyone tells me what I have been doing wrong here. Thank you!
NOTE This anwer relates to the initial version of the question.
Variables node->name
and node->telephone
are not pointers to separate allocated blocks of memory, they're just parts of *node
. Reduce the freeTelephoneBookNode
function to
void freeTelephoneBookNode(TelephoneBookNode * node) {
free(node);
}