Originally Posted by
iMalc
Hadn't looked close enough to notice that. Yeah that's a bad thing to do. If you're lucky it might work a few times, but then it'll go boom sooner or later. You must not access or write to a structure after it has been freed.
In this case you can just delete this line:
Code:
cliPtr->clientProfile = NULL;
There's no point setting something to NULL when it's going out of existence.
Hum... When I use free on something and if in the next line I try to print it's value anyways, the value will still be there, that's why I set the variable to NULL. To make sure it's cleaned.
I guess free cleans that memory block and the data will still be there until something else overwrites it and it's the day it's supposed to work, right?
Hum... After searching a bit for iteration on binary trees I found an algorithm for iterative insert, here's what I did:
Code:
void addClientToTree(ClientTree *cTree, ClientProfile cProfile) {
if(!*cTree) {
*cTree = newClientTreeNode(cProfile);
return;
}
ClientTree currPtr = *cTree;
int nCompare;
do {
nCompare = strcmp(currPtr->clientName, cProfile->clientName);
if(nCompare > 0) {
if(!currPtr->leftTree) {
currPtr->leftTree = newClientTreeNode(cProfile);
return;
} else {
currPtr = currPtr->leftTree;
}
} else if(nCompare < 0) {
if(!currPtr->rightTree) {
currPtr->rightTree = newClientTreeNode(cProfile);
return;
} else {
currPtr = currPtr->rightTree;
}
}
} while(currPtr && nCompare != 0);
return;
}
Code:
ClientTree newClientTreeNode(ClientProfile cProfile) {
ClientTree newClient = malloc(sizeof(nClientTree));
if(!newClient) perror("malloc");
newClient->clientName = strdup(cProfile->clientName);
if(!newClient->clientName) perror("strdup");
newClient->clientProfile = cProfile;
newClient->leftTree = NULL;
newClient->rightTree = NULL;
return newClient;
}
I guess it's ok now?
I'll try to convert my delete function into iteration and see if I can also delete a nod with 2 child's...