Thread: Infinite Loop in Destructor...

  1. #1
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23

    Unhappy Infinite Loop in Destructor...

    Shouldn't this become NULL and exit the loop? It certainly doesn't...

    Code:
    void DestroySymTab(struct SymTab *ATable) {
    	int i;
    	struct SymEntry *p;
    	
    	for (i = 0; i < ATable->Size; i++) {
    		while(ATable->Contents[i] != NULL) {
    			p = (struct SymEntry*)ATable->Contents[i];
    			while(p->Next != NULL){
    				p = p->Next;
    			}
    			free(p);
    			p = NULL;
    		}
    	}
    	free(ATable);
    };

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that your linked list is actually NULL-terminated, and doesn't have a loop somewhere?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by P3nGu1N View Post
    Shouldn't this become NULL and exit the loop?
    Only if one day you have a Next that is NULL already.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Well, right now for testing purposes I haven't implemented adding anything past one name, and SymEntry->Next should be NULL terminated for that one.

    Code:
    bool EnterName(struct SymTab *ATable, const char *Name,
    					struct SymEntry * *AnEntry) {
    	int hashValue = hash(Name,ATable);
    	char n[200];
    	int nameLength;
    	struct SymEntry *currentEntry;
    	
    	strcpy(n, Name);
    	nameLength = strlen(n) + 1;
    	
    	if(ATable->Contents[hashValue] == NULL) {
    		currentEntry = (struct SymEntry*)malloc(sizeof(struct SymEntry*) + nameLength);
    		currentEntry->Name = n;
    		currentEntry->Next = NULL;
    		ATable->Contents[hashValue] = currentEntry;
    		AnEntry = &currentEntry;
    		return false;
    	}
    	
    	return true;
    };
    I think it shouldn't even touch that second while loop?... maybe I should comment it out and see if it makes a difference.
    Last edited by P3nGu1N; 03-06-2009 at 09:58 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Have you tried one of these inside that while loop:
    Code:
    printf("address: %p\n",p->Next); fflush(stdout);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, there's a different problem:
    Code:
    		while(ATable->Contents[i] != NULL) {
    			p = (struct SymEntry*)ATable->Contents[i];
    			while(p->Next != NULL){
    				p = p->Next;
    			}
    			free(p);
    			p = NULL;
    		}
    If the red bit is true the first iteration, what changes it later?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Tried it, and nope the infinite loop is in the outer while statement.

    Code:
    void DestroySymTab(struct SymTab *ATable) {
    	int i;
    	struct SymEntry *p;
    	
    	for (i = 0; i < ATable->Size; i++) {
    		while(ATable->Contents[i] != NULL) {
    			p = (struct SymEntry*)ATable->Contents[i];
    			/*while(p->Next != NULL){
    				p = p->Next;
    			}*/
    			free(p);
    			p = NULL;
    		}
    	}
    	free(ATable);
    };

  8. #8
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Quote Originally Posted by matsp View Post
    No, there's a different problem:
    Code:
    		while(ATable->Contents[i] != NULL) {
    			p = (struct SymEntry*)ATable->Contents[i];
    			while(p->Next != NULL){
    				p = p->Next;
    			}
    			free(p);
    			p = NULL;
    		}
    If the red bit is true the first iteration, what changes it later?

    --
    Mats
    Shouldn't Contents[i] be set to NULL when I set the pointer p to NULL when it's pointing to it?

    Okay, trying the other suggestion, bbs.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by P3nGu1N View Post
    Tried it, and nope the infinite loop is in the outer while statement.
    So you mean the inner loop never happened because the first pointer is NULL? Check the address of ATable->Contents[i] the same way...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Alright, well it keeps spitting out address:0 in the console, let me try the Contents[i] alone one for you.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by P3nGu1N View Post
    Shouldn't Contents[i] be set to NULL when I set the pointer p to NULL when it's pointing to it?

    Okay, trying the other suggestion, bbs.
    p is not changing anything other than p.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by P3nGu1N View Post
    Alright, well it keeps spitting out address:0 in the console, let me try the Contents[i] alone one for you.
    0 is not NULL, at least.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    Alright, just tried this:
    Code:
    void DestroySymTab(struct SymTab *ATable) {
    	int i;
    	struct SymEntry *p;
    	
    	for (i = 0; i < ATable->Size; i++) {
    		while(ATable->Contents[i] != NULL) {
    			/*p = (struct SymEntry*)ATable->Contents[i];*/
    			printf("address (Contents[i]): %p\n",ATable->Contents[i]); fflush(stdout);
    			/*while(p->Next != NULL){
    				p = p->Next;
    			}*/
    			free(p);
    			p = NULL;
    		}
    	}
    	free(ATable);
    };
    and it keeps spitting out address (Contents[i]): ffe0b18, which is a valid memory address I'm guessing.

    If that's not how I set p to point to Contents[i], is it supposed to be *p = (struct SymEntry*)ATable->Contents[i]; instead of just p = (struct SymEntry*)ATable->Contents[i]; ?

    I'm seriously bad at this stuff, sorry if you feel like I'm wasting your time.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    0 is not NULL
    It can be, or at least 0 == NULL is true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by P3nGu1N View Post
    I'm seriously bad at this stuff, sorry if you feel like I'm wasting your time.
    I choose to waste it...anyway, the loop is infinite because you are basically saying
    Code:
    int x=5;
    while (x==5) {
          blah blah
    }
    If ATable->Contents[i] was not NULL to start with (opening the loop) there is nothing in the loop that affects this pointer, as far as I can tell.

    It looks like you just want to iterate through the array, which would mean the while() inside the for() is superfluous. I think you should choose one or the other.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. question ab an infinite loop...
    By JohnMayer in forum C Programming
    Replies: 10
    Last Post: 07-26-2002, 10:15 AM