free () problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

free () problem

 
0
  #1
Apr 6th, 2007
I defined a struct that has the format:

  1. struct node{
  2. char *str;
  3. int key;
  4. link next;
  5. };

and I have a list of this type, when I try to do this
  1. free ( list -> str )
the program terminates :-| how can I free the memory pointed by list->str without the program terminating?

Thanks in advance
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: free () problem

 
0
  #2
Apr 6th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: free () problem

 
0
  #3
Apr 6th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: free () problem

 
0
  #4
Apr 9th, 2007
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:

  1. typedef struct node *link;
  2. struct node{
  3. char *str;
  4. int key;
  5. link next;
  6. };

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.
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: free () problem

 
0
  #5
Apr 9th, 2007
>>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.
Last edited by Lerner; Apr 9th, 2007 at 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: free () problem

 
0
  #6
Apr 9th, 2007
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.
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: free () problem

 
1
  #7
Apr 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: l-o-s-t is an unknown quantity at this point 
Solved Threads: 0
l-o-s-t's Avatar
l-o-s-t l-o-s-t is offline Offline
Newbie Poster

Re: free () problem

 
0
  #8
Apr 10th, 2007
Thanks ... I think I know how to handle that problem now ... you made me aware of things I ignore before ... thanks alot
(¯`·._.· Swaying with the wind ·._.·´¯)

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC