C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 10th, 2003, 06:56 PM
dmittner dmittner is offline
Dazed&Confused
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tempe, AZ
Posts: 91 dmittner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 34 m 13 sec
Reputation Power: 7
Send a message via ICQ to dmittner Send a message via AIM to dmittner
Freeing/NULLing Problem

Okay this one just seems really bizarre, considering that I've been around C (albiet not indepth) for years and have never come across this problem until now.

I have a global linked list of say, typedef 'TEST_STRUCT'.
I have a function that pulls a single element of the list and places it into a local pointer of the same struct type.

If I loop through and free all of the linked list elements from memory, shouldn't there be some way for that local pointer to know it's now NULL?

Actually I guess I never came upon that before because past code environments recycled memory instead of freeing it, but this seems odd, none-the-less. So I guess I'm just wondering if there's a way for that local pointer to know what has happened, or do I just need to do away with the local variable?

Any advice is appreciated.

Reply With Quote
  #2  
Old June 11th, 2003, 03:57 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
Its termed a 'dangling pointer'.
One that no longer points to the memory originally assigned to it (because that memory has been freed or realloced).

Why are you not refreshing the local pointer when it tries to use the memory again? (as the local pointer will go out of scope as soon as the function exits)

When you free a dynamicly allocated memory pointer do you set it to NULL? (so the next time the local pointer calls on it the returned address will be NULL)
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #3  
Old June 11th, 2003, 12:07 PM
dmittner dmittner is offline
Dazed&Confused
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tempe, AZ
Posts: 91 dmittner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 34 m 13 sec
Reputation Power: 7
Send a message via ICQ to dmittner Send a message via AIM to dmittner
Well... if there's a term for it, that's pretty much all I needed to know. I was just curious if C might have gone through all the pointers in the program and nullified them when what they're pointing to is freed or something.

This question was kind of academic anyway, since the problem can be avoided in my code.

To answer your questions, though:

Refreshing the pointer is definately a fix, but I was intentionally avoiding that in order to eliminate a function call. No matter... it's not as though the called function is a heavy one.

And yeah, I tried setting it to NULL when it was freed. My guess is that the system was quick to place something new in the hole, invalidating the NULL value.

Anyway.. like I said, you pretty much answered my question. Thanks for the insight.

Reply With Quote
  #4  
Old June 11th, 2003, 02:08 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,836 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 7 h 41 sec
Reputation Power: 446
Quote:
Originally posted by dmittner
Well... if there's a term for it, that's pretty much all I needed to know. I was just curious if C might have gone through all the pointers in the program and nullified them when what they're pointing to is freed or something.

C doesn't believe in coddling its programmers nor in protecting them against themselves. It assumes that you must know what you're doing so if you tell it to do something it just figures that that is what you want and does it. That also means that it doesn't automatically clean up after you, but rather you need to do it yourself. Of course, some libraries may do more of that stuff for you, but then that's another programmer at work.

Quote:
Originally posted by dmittner
And yeah, I tried setting it to NULL when it was freed. My guess is that the system was quick to place something new in the hole, invalidating the NULL value.

How was that local pointer declared? Local variables are of the auto storage class by default. That means that they are allocated on the stack and they go away when you exit the function. Then when you come back, they'll contain whatever was previously written in those memory locations, AKA "garbage" -- exception being the ones that are explicitly initialized.

However, if you declare a local variable to be static, then it is stored in memory and it retains its value between function calls. The only thing is that it can only be access from within that function.

Reply With Quote
  #5  
Old June 11th, 2003, 02:34 PM
dmittner dmittner is offline
Dazed&Confused
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tempe, AZ
Posts: 91 dmittner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 34 m 13 sec
Reputation Power: 7
Send a message via ICQ to dmittner Send a message via AIM to dmittner
Quote:
Originally posted by dwise1_aol
C doesn't believe in coddling its programmers nor in protecting them against themselves. It assumes that you must know what you're doing so if you tell it to do something it just figures that that is what you want and does it. That also means that it doesn't automatically clean up after you, but rather you need to do it yourself. Of course, some libraries may do more of that stuff for you, but then that's another programmer at work.


Granted. I'm use to Perl which makes things quite easy on the programmer, and most of my past C work was done in code that already had safeguards in place. I'm kind of coming out into the real world of C now.

Quote:
How was that local pointer declared? Local variables are of the auto storage class by default. That means that they are allocated on the stack and they go away when you exit the function. Then when you come back, they'll contain whatever was previously written in those memory locations, AKA "garbage" -- exception being the ones that are explicitly initialized.

However, if you declare a local variable to be static, then it is stored in memory and it retains its value between function calls. The only thing is that it can only be access from within that function.


The process of code was essentially this, and keep in mind this is a dumbed down version and can easily be avoided:

Code:
{
    TEST_DATA *td;
    td = routineToPullPointerFromLinkedList("nameToMatch");
    routineToFreeAllLinkedListElements();
    if ( td == NULL )
        td = routineToCreateNewElement("name" ... );

    [...]
}


Unfortunately I couldn't even get that NULL check to apply -- as though 'td' wasn't NULL even despite an exlicit declaration of such in the freeing function.

Reply With Quote
  #6  
Old June 11th, 2003, 03:27 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,836 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 7 h 41 sec
Reputation Power: 446
Quote:
Originally posted by dmittner
The process of code was essentially this, and keep in mind this is a dumbed down version and can easily be avoided:

Code:
{
    TEST_DATA *td;
    td = routineToPullPointerFromLinkedList("nameToMatch");
    routineToFreeAllLinkedListElements();
    if ( td == NULL )
        td = routineToCreateNewElement("name" ... );

    [...]
}


Unfortunately I couldn't even get that NULL check to apply -- as though 'td' wasn't NULL even despite an exlicit declaration of such in the freeing function.

OK, let's assume that routineToPullPointerFromLinkedList() returned a valid pointer, so that's what's been stored in td. That pointer address remains in td after routineToFreeAllLinkedListElements() is called. So, of course, td does not equal NULL.

OK, it's just that it sounded like something else was going on instead. My mistake.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Freeing/NULLing Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway