Thread: Linked List

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Working is not the same as semantically correct. You are basically writing a reversed linked list.
    What do you mean you don't need to? You need to free everything you malloc. I don't see a single free statement in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    haha, all right.
    I can not retort with any excuse that I did not free the malloc
    And one more question:
    How could I know if my malloc has been freeed ?
    with code below ( no more argue about the way of inserting, only concern the free malloc problem)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct list_el
    {
    	int val;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(int argc, char **argv)
    {
    
    	item *curr, *head,*tmp;
    	int i;
    
    	 curr = head = tmp = NULL;
    
    	 for(i=1; i<=5; i++)
    	 {
    
    		 curr = (item*)malloc(sizeof(item));
    		 curr->val = i;
    
    		 curr->next = head;
    		 head = curr;
    
    	 }
    	  head = curr;
    
    
    	 while(curr)
    	  {
    
    		  printf("\n %d",curr->val);
    		  tmp = curr;
    		  curr = curr->next;
                      free(tmp);
    		 
    
    	  }
    
    	return 0;
    }

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Easiest way is to use a tool such as valgrind. It will detect memory leaks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    If I try to free memory as in shown in the snippet, I get warning stating "Invalid Address specified to RtlFreeHeap" and "Program Received signal SIGTRAP".

    Can anybody suggest correct way of freeing memory ??

    Code:
    /*Program constructs an linked list of 5 members and displays it  */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int x;
    	node *next;
    };
    int main(int argc, char **argv)
    {
    	struct node *head, *curr, *temp;
    	int i;
    	head = (struct node*)malloc(sizeof(struct node));
    	
    	curr = head;
    	
    	for(i=1;i<=5;i++)
    	{
    	
    	curr -> next = (struct node*)malloc(sizeof(struct node));
    	curr -> x= i;
    	temp=curr;  
    	curr = curr->next;
    	}
    	temp->next =NULL;
    	
    	
    	
    	//Display members
    	curr = head;
    
    	
    	while(curr->next!=NULL) 
    	{
    		printf("\n %d", curr->x);
    		curr = curr->next;
    		temp = curr;
    		free(temp);
    	}
    	printf("\n %d", curr->x); // to print the last element in the list
    	
    	return 0;
    }

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    		curr = curr->next;
    		temp = curr;
    Statements are mixed up.
    You're advancing the pointer, freeing it, then on the next iteration, you're trying to access the very same pointer you just freed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    switch the order of statements below:
    Code:
    curr = curr->next;
    temp = curr;
    I tried the whole program, looks fine.
    Code:
    /*Program constructs an linked list of 5 members and displays it  */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int x;
    	struct node *next;
    };
    int main(int argc, char **argv)
    {
    	struct node *head, *curr, *temp;
    	int i;
    	head = (struct node*)malloc(sizeof(struct node));
    
    	curr = head;
    
    	for(i=1;i<=5;i++)
    	{
    
    	curr -> next = (struct node*)malloc(sizeof(struct node));
    	curr -> x= i;
    	temp=curr;
    	curr = curr->next;
    	}
    	temp->next =NULL;
    
    
    
    	//Display members
    	curr = head;
    
    
    	while(curr->next!=NULL)
    	{
    		printf("\n %d", curr->x);
    		temp = curr;
    		curr = curr->next;
    		free(temp);
    	}
    	printf("\n %d", curr->x); // to print the last element in the list
    
    	return 0;
    }

  7. #22
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Post

    But if I try to execute this program I get "Program Received signal SIGTRAP" warnig and I do not get any output?

    So my question is, should I apply another loop to free the memory, I mean some thing llike below snippet?

    With this loop, I got the output but debugger hangs and I still receive the warning stating"Program Received signal SIGTRAP".

    Can anbody please suggest any solution to this problem?

    Code:
    curr = head;
    
    while(curr->next!= NULL)
    {
          free(curr);
          curr = curr->next;
    }

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Once you free(curr), it follows that you should not write curr->next.

    Furthermore, it may well be the case that curr is a null pointer, hence writing curr->next in the loop condition could be wrong.

    Rather think along the lines of: while curr is not a null pointer, get the next pointer, then free curr, and then make the next pointer curr... rinse, wash, repeat.
    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

  9. #24
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Thumbs up

    Do you mean something like this:
    Code:
    curr =head;
    while(curr!= NULL)
    {
         curr =curr->next;
         free(curr);
    }
    
    free(curr); // free the tail
    curr = head;
    free (curr);
    I tried this way, I mean I get output along with the warning"Program Received signal SIGTRAP" and debugger hangs. Is this an right way of freeing memory ?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're close, but think: if you assign curr->next to curr, then free curr, what you're freeing would be the original curr->next, not the actual curr that you want to free.
    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

  11. #26
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    This works :
    Code:
    curr = head;
    while(curr!= NULL)
    {
         
    	temp =curr->next;
    	free(curr);
    	curr = temp;
    }
    
    free(curr); // free the tail
    Thanks for the help.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that works. The last free(curr) is probably unnecessary, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I think it is necessary since the while loop is curr != NULL, so it is not going to free the last member with curr->next =NULL.

  14. #29
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by OCcounty View Post
    I think it is necessary since the while loop is curr != NULL, so it is not going to free the last member with curr->next =NULL.
    No. In order for the loop to break out the condition must be false, therefore curr will be NULL. Freeing NULL is not advisable.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If curr != NULL, then it will save curr->next in curr and free curr.
    If cur->next != NULL at this point, then curr != NULL on the next loop, and hence it will be freed to.
    If cur->next == NULL, the loop terminates and cur == NULL.
    According to that logic (I could be wrong), it should not be necessary to use the last free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread