I defined a struct that has the format:

struct node{ 
                   char *str;
                   int     key;
                   link    next;
                 };

and I have a list of this type, when I try to do this

free ( list -> str )

the program terminates :-| how can I free the memory pointed by list->str without the program terminating?

Thanks in advance

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.

> 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.

I guess the problem was trying to free a string that only consisted of "\0" ... that gave me an error ... anyway, I got a bigger problem which has to do with this struct:

typedef struct node *link;
struct node{ 
                   char *str;
                   int     key;
                   link    next;
                 };

I have defined in the main function ( link list ) and i pass that to a void function that meant to manipulate the list by passing to it ( link list ), i can get it for example to delete or rearrange nodes but 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.

>>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.

Yeah but this is how it was meant to be called, it is part of the specs, it is not up to me to change the function prototype, beside it worked for other cases like the rest of the list, when i access the list from main after calling the function, the content of the list is changed, so I assume what I am doing is right, however when it comes to changing the first node, it never works, it makes me wonder wether all of the things i did are right in the first place.

Can someone please tell me how to deal with this void function in such a way that i can change the content and/or the location of the first node.

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.

Thanks ... I think I know how to handle that problem now ... you made me aware of things I ignore before ... thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.