Why my code will stop running? Thanks!

This is a discussion on Why my code will stop running? Thanks! within the C Programming forums, part of the General Programming Boards category; When I start to run my code, it will stop anyway, why? Thanks for any help! Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    12

    Why my code will stop running? Thanks!

    When I start to run my code, it will stop anyway, why? Thanks for any help!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct _list_item {
        int value;
        struct _list_item* next;
    } list_item;
    
    void del_item(list_item **root, int val);
    void print_list(list_item *root);
    
    int main(void)
    {
    	list_item list[6],*lptr;
    	
    
    	list[0].value=1; list[0].next=list+1;
    	list[1].value=2; list[1].next=list+2;
    	list[2].value=3; list[2].next=list+3;
    	list[3].value=4; list[3].next=list+4;
    	list[4].value=5; list[4].next=list+5;
    	list[5].value=6; list[5].next=0;
    
    	printf("test to delete list item whose value is 3\n");
    	del_item(&list, 3); // Wrong?
    	print_list(list);
    	return 0;
    }
    
    void del_item(list_item **root, int val)
    {
    	list_item *currP, *prevP;
    
    	  /* For 1st node, indicate there is no previous. */
    	  prevP = NULL;
    
    	  /*
    	   * Visit each node, maintaining a pointer to
    	   * the previous node we just visited.
    	   */
    	  for (currP = *root;
    		currP != NULL;
    		prevP = currP, currP = currP->next) {
    
    	    if (currP->value == val) {  /* Found it. */
    	      if (prevP == NULL) {
    	        /* Fix beginning pointer. */
    	        *root = currP->next;
    	      } else {
    	        
    	         /* Fix previous node's next to
    	         * skip over the removed node.
    	         */
    	        prevP->next = currP->next;
    	      }
    
    	      /* Deallocate the node.*/ 
    	      free(currP);
    
    	      /* Done searching. */ 
    	      return;
    	    }
    	  }
    	
    }
    void print_list(list_item *root)
    {
    	list_item* lp=root;
    	while(lp) {
    		printf("%d\n", lp->value);
    		lp=lp->next;
    	}
    	
    }
    Last edited by ljin; 08-24-2009 at 08:25 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can never never never never free a bit of an array. If it didn't come from malloc, you must not free it.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    60
    it just a leak of memory.....gr

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    12
    it still wont work.

    Quote Originally Posted by tabstop View Post
    You can never never never never free a bit of an array. If it didn't come from malloc, you must not free it.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    12
    Guess so... how come will that happen?
    Quote Originally Posted by bvkim View Post
    it just a leak of memory.....gr

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're driving to a new friend's house for the first time. She has an unlisted phone number, which you don't have memorized. You have her phone number and address, just on a slip of folded up paper.

    When a gust of wind from a passing truck blows that piece of paper out the window, how will you find her house?

    When you free a memory address, the address is removed from the list of available addresses, until your program quits, or you reboot (depending on the OS).

    You might be able to add that memory address back into the list (table), via some assembly commands. I'm unclear on that.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    You need to call malloc for each of your nodes.
    Not declare a local array of nodes and then attempt to free them later.

    You can only get a memory leak if you call malloc AND then don't call free sometime later.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by bvkim View Post
    it just a leak of memory.....gr
    No, there isn't any memory being dynamically allocated so nothing is leaking there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  3. Running Java code on my mobile phone
    By siavoshkc in forum Tech Board
    Replies: 14
    Last Post: 09-11-2006, 01:57 AM
  4. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  5. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM

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