how was it initialized and allocated (new or malloc() ? Is list or str NULL pointers ? Hard to say what the problem is without more code.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> how can I free the memory pointed by list->str without the program terminating?
By fixing the bugs elsewhere in your program.
What you're experiencing here is cause and effect.
The effect is a crash in free which on the face of it seems an entirely correct and plausible thing to do.
The cause is a prior undetected bug which is earlier on in your code. Maybe it trashed the heap without actually crashing itself, and then on the next heap function, that's when all the 'fun' starts.
As AD says, more code is needed for a better answer.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>>when I try to manipulate the first node in the list, nothing happens to the first node in the list back main function, so it seems to be changing that locally, however main doesn't seem to acknowledge these changes ... could anyone please tell me how can you change the head of the list if the function is of a void type.
Sounds like a situation that calls for passing by reference instead of passing by value.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
If you change the first node (or there is any possibility that you could), you need to return that to the caller.
Eg.
list = appendToList( list, data );
Or
appendToList ( &list, data );
Sure, 99% of the time, the list head doesn't change, but that doesn't help you at all when it really matters.
If you can't change the interface to your list functions, then the interface is broken.
You could also create a DUMMY first node which is always at the head of the list, but that creates problems all over the place as each bit of code has to be aware that a dummy is present.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953