Like Tree3Likes
  • 3 Post By AndrewHunter

DEBUG ERROR WHEN USING free() ??

This is a discussion on DEBUG ERROR WHEN USING free() ?? within the C Programming forums, part of the General Programming Boards category; Hey guys. So i have a program that uses linked lists, and it can insert, delete, and print the list. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Post DEBUG ERROR WHEN USING free() ??

    Hey guys. So i have a program that uses linked lists, and it can insert, delete, and print the list. My insertion works just fine, but there seems to be an error with the deletion. XD

    Code:
    void deleteNode(link *head, int input)
    {
    	link p;
    	link curr;
    	link dealloc;
    
    	int match=0;
    
    	p=*head;
    
    	while(p!=NULL)
    	{
    		if(p->x==input)
    			match++;
    		p=p->next;
    	}
    
    	if(match>=1)
    	{
    	
    	     if((*head)->x == input)
    	    {	
    		  dealloc=*head;
    		  *head=(*head)->next;
    		  free(dealloc);
    	     }
    	
    	     else
    	     {
    		    p=*head;
    		    curr = p->next;
    
    		    link temp;
    
    		    while(curr!=NULL && curr->x!=input)
    		    {
    			    p = p->next;
    			    curr = p->next;
    		    }
    		
    		    p->next=curr->next;
    		    free(curr);
    	    }
    
    	    printList(head);
    
    	}
    
    	else
    	{
    		printf("NUMBER NOT FOUND!");
    		printList(head);
    
    	}
    }
    }
    If I dont use free(), the program works well.

    When I try to use free(), a window appears and says something about a debug error. Can you figure out what's wrong? How do I fix this? Thank you
    Attached Files Attached Files
    Last edited by mugiwara528; 07-13-2011 at 09:28 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    You can only free addresses that was allocated.

    This is VERY likely wrong.
    Code:
    link dealloc;
    I think it should be
    Code:
    link *dealloc;
    Edit: This was wrong advise because link is a pointer to the node.

    Tim S.
    Last edited by stahta01; 07-13-2011 at 09:41 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    So I need to put something like dealloc = (link) malloc(sizeof(link)); in my function?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    Quote Originally Posted by mugiwara528 View Post
    So I need to put something like dealloc = (link) malloc(sizeof(link)); in my function?
    ???, read my edit of my prior post.

    Tim S.
    Last edited by stahta01; 07-13-2011 at 09:20 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Actually i used

    Code:
    typedef struct node link1;
    typedef link1 *link;

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    Quote Originally Posted by mugiwara528 View Post
    Actually i used

    Code:
    typedef struct node link1;
    typedef link1 *link;
    OK, did not realize link was a pointer. Not seeing any problem.
    But, I find you code hard to follow; you might wish to indent it better.

    Tim S.
    Last edited by stahta01; 07-13-2011 at 09:35 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Actually there is, when I try to delete a window pops up and says something like
    " Debug Error!

    Program: blablabla

    DAMAGE: after Normal block (#63) at 0x00431D20
    "
    Then i have to choose between Abort, Retry or Ignore.

    If i choose Ignore the program runs fine but everytime I delete sumthing the window pops up again. :\

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,688
    If link is a pointer you ARE NOT allocating any space to store data.
    Code:
    temp=(link)malloc(sizeof(link));
    Edit: Link lists require you to allocate the space for each node NOT just a pointer to the node.

    Tim S.
    Last edited by stahta01; 07-13-2011 at 09:34 AM.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here is a very good tutorial on linked lists. It will help you with your implementation and thus avoid these errors.

    @Stahta01 - not hijacking, OP's code makes my head hurt.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    It worked! I'm not sure though if what I did was right...is it???

    Code:
            link1 *dealloc;
    
            dealloc=(link1 *)malloc(sizeof(link1));
    
    
    	if((*head)->x == input)
    	{	
    		
    		dealloc->x=(*head)->x;
    		dealloc->next=*head;
    		*head=(*head)->next;
    		free(dealloc);
    	}
    	
    	else
    	{
    		p=*head;
    		curr = p->next;
    
    		link temp;
    
    		while(curr!=NULL && curr->x!=input)
    		{
    			p = p->next;
    			curr = p->next;
    		}
    		
    		dealloc->x=p->x;
    		dealloc->next=p->next;
    		p->next=curr->next;
    		curr=curr->next;
    		free(dealloc);
    	}
    Thank you, by the way.
    *bows*

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I can pretty much guarantee that no that's not right. If this is code that is supposed to add something to the list, then you must not free the pointer, because then you don't have the thing you just added any more. If this is code that is supposed to delete something from the list, then you must not malloc anything, because you're not trying to create something.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    This function is to remove a node from the list and free the memory allocated to that certain node, so i guess i need to use free(), am i right? What do I do?? Do i pass call malloc in my main and pass it as a parameter to deleteNode??

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't call malloc at all, in that situation. Why would you call malloc? You don't need to create a new node in order to delete a node, and it is wrong to do so.

    (ETA: The malloc calls need to happen when you build the list.)

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    A linked list is simply a group of structures (containers to be exact, you can have a series of class objects) that have at least one member that is a pointer to the next object in the list. Now this isn’t as complicated as it may sound, lets take a look at it piece by piece.

    1. A linked list is a group of structures; thus one element in the linked list(often referred to as a node) would be a structure. So an example of this node would be similar to:
    Code:
    struct myNode {
        char name[10];
        char phoneNumber[13];
    };
    2. These nodes must contain at least one member that is a pointer to the next structure. Now we say at least one member because often it is more convenient to have 2 pointers; one pointing to the next node and one pointing to the previous node. So we know we need a pointer that is of type of our structure, thus something like this:
    Code:
    struct myNode* nextNode;
    So putting this together we get the definition of our node:
    Code:
    struct myNode {
        char name[10];
        char phoneNumber[12];
        struct myNode* nextNode;
    };
    Now that we understand the concept of what a node is lets move onto the implementation of the linked list. As stated previously a linked list is a group or chain of nodes. As with all chains we need to define an anchor or starting point. This starting point is simply a pointer that is of type of our structure. So our implementation of this anchor would be like:
    Code:
    struct myNode* firstNode;
    You define the end of a linked list by having the last node’s next pointer set to NULL. Now in the beginning of our program to signify an initially empty list we will set firstNode to NULL. Thus you will usually see something along the lines of:
    Code:
    firstNode = NULL;
    Alright the remaining concept to grasp the fundamentals of linked lists lies in successfully adding and removing nodes from our list. These topics revolve around dynamic memory allocation, using malloc() and free().This topic is as easy to understand as the previous topics however it seems to be the hardest for new programmers to grasp so we’ll spend some time here. The prototype for malloc() is:
    Code:
    void* malloc(size_t size);
    All malloc() does is allocate size bytes and returns a pointer of type void to that memory, or NULL if an error occured. So you are probably asking, well that’s great but how do I use it? Well the standard convention for using malloc is:
    Code:
    pointer = malloc(n * sizeof(*pointer));
    where n is the number of elements you need and *pointer (the dereferenced pointer) is used to determine the size of the individual element. So if I wanted to create 1 new node (our node we have been using) I would do it like so:
    Code:
    //allocate space for our node
    newNode = malloc(1 * sizeof(*newNode));
    //check for error
    if(newNode == NULL){
         printf("Error allocating memory");
         return;
    }
    *Note, the ISO standard dictates there is no need to cast the return from malloc(), however some compilers may complain about being unable to convert the types. If this happens you can still use malloc, simply cast the return*

    Now that we understand how to allocate the memory, lets take a look at how we add the node to our list. Remember that we signify the end of our list by having the last node’s next node pointer be NULL. Thus to add a new node we simply “walk through” our linked list until we find the last node. Once we are there we set the last node’s next node pointer to our newly created node. Thus the implementation would look like:
    Code:
    //first check if the linked list is empty
    //if it is make the new node the first node
    if(firstNode == NULL) {
         firstNode = newNode;
         return;
    }
    //if not find the last node
    walker = firstNode;
    while(walker->nextNode != NULL){
         walker = walker->nextNode;
    }
    //set last node's next node to our new node
    walker->nextNode = newNode;
    To delete a node is just as easy as adding a node but before we dive into that lets take a look at how we free the memory we allocated with malloc(). The function is conveniently called free() and its prototype is:
    Code:
    void free(void* mem);
    where mem is a pointer to the block of memory allocated dynamically. Note that there is no return value and free does not ask for nor want a size to free. It simply releases the amount of memory pointed to by mem. Pretty easy, right?

    So now you are probably asking, well that’s great but how do I delete my node from the list? Remember that these lists are chained together simply by the pointers that each element has. So to remove an element from the list all you need to do is:
    1. Find the node to delete and the previous node
    2. Make the previous node’s next node pointer point to the node following the node to delete (aka set the previous node’s next node member equal to the node to delete’s next node member).
    3. free the memory allocated for the node to delete.

    So lets look at this 1 step at a time:
    1. Find the node to delete and the previous node
    Code:
    //if deleting the first node
    if(nodeNumber == 1){
         nodeToDelete = firstNode;
         firstNode = firstNode->nextNode;
    }else{
         //start at the begginning
         nodeToDelete = firstNode;
         prevNode = NULL;
         //find the previous node and node to delete
         for(int i = 0; i < nodeNumber - 1; i++){
              prevNode = nodeToDelete;
              nodeToDelete = nodeToDelete->nextNode;
         }
    2. Make the previous node’s next node pointer point to the node
    Code:
    prevNode->nextNode = nodeToDelete->nextNode;
    3. free the memory allocated for the node to delete.
    Code:
    free(nodeToDelete);
    And that is all there is to removing a node from a linked list and about all the basics for a linked list.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mugiwara528 View Post
    This function is to remove a node from the list and free the memory allocated to that certain node, so i guess i need to use free(), am i right? What do I do?? Do i pass call malloc in my main and pass it as a parameter to deleteNode??
    I don't think you're catching the dynamic here... You use malloc() to create space for your data. You use free() to release the memory when you're done with the data. You do not malloc space, not use it then free it... that's nothing more than wasted CPU time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Help! :D
    By CodyHerrick in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2009, 08:36 PM
  2. Help me debug error, please!
    By phuong_bk in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2009, 07:19 AM
  3. Please debug the error!
    By madhusudhan in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:42 AM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. Trying to Free Object On Heap As Array Throwing Debug Asserts
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2007, 10:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21