Thread: Dynamic memory, free()

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Dynamic memory, free()

    Hello,

    I'm trying to write a program which deletes the last element in a linked list of structures. However, I'm stuck with free, I know how it works but free() always give problems, any help?

    This is my code:
    Code:
    /* deletes last element */
    void delete_last (LINKED* *list)
    {
    	LINKED *p;
    	LINKED *q;
    
    
    	p = *list; // initialize pntr
    	
    	if (*list == NULL) // control for empty list
    		printf("EMPTY LIST.\n");
    	else if ((*list)->pntr == NULL) // control for 1 element
    		free((*list)->pntr);
    	else
    	{
    		while (p->pntr != NULL)
    		{
    			q = p;
    			p = p->pntr;
    		}
    
    
    		free(q);
    		q = NULL;
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If there is only one element, then you should be freeing the memory for that element and then set the pointer to that element to be a null pointer. If there is more than one element, then you should be freeing the memory for the last element, then set the next pointer for the previous element to be a null pointer.
    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

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Oh yes, I see the problem when I have one element. But with more elements it should work, doesn't it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, since the loop should iterate at least once, though I'm still uncomfortable not initialising q first. (Naming q previous might be better, and what's with pntr when next would be a much better name?)

    That said, note that setting q to be a null pointer only changes the pointer local to the function.
    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

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    No, q also changes the value in list. Note that p = *list, and after that q = p.
    And indeed, it'd be better to initialise q, also the names could be better.
    But the matter is that the loop doesn't work...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kevinstrijbos
    No, q also changes the value in list. Note that p = *list, and after that q = p.
    (...)
    But the matter is that the loop doesn't work...
    You're free to believe what you want to believe. All that will happen is that the loop will continue to appear to not work

    EDIT:
    Actually, I suspect that I'm also getting confused as to what is p and q. Silly undescriptive names. I'll write this thing out myself to make it clearer.
    Last edited by laserlight; 02-09-2012 at 10:56 AM.
    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

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Well, after a lot of trouble with ctrl+f I managed to change the names!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, okay. I understand now. Indeed, this is wrong:
    Code:
    free(q);
    q = NULL;
    It should be:
    Code:
    free(p);
    q->nptr = NULL;
    free(q) is wrong because q is the previous element. It is not the last element. The assignment of NULL to q is clearly insufficient because q is a local variable. Yes, it points to an element in the linked list, but setting it to be a null pointer just means that it no longer points to an element in the linked list. It does not change any element in the linked list.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does free() know how much memory it has to free?
    By shiroaisu in forum C Programming
    Replies: 14
    Last Post: 09-09-2011, 11:28 AM
  2. Dynamic two dimensional array free problem
    By nickman in forum C Programming
    Replies: 14
    Last Post: 01-13-2011, 12:44 PM
  3. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  4. Issues with free and dynamic memory
    By dan s in forum C Programming
    Replies: 3
    Last Post: 01-14-2007, 03:44 AM
  5. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM