Am I forgetting any cleanup/safety checks that I should be using? (Linked lists)

This is a discussion on Am I forgetting any cleanup/safety checks that I should be using? (Linked lists) within the C++ Programming forums, part of the General Programming Boards category; I was just wondering if in this code (which works) I am forgetting anything to clean up / protect myself ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Am I forgetting any cleanup/safety checks that I should be using? (Linked lists)

    I was just wondering if in this code (which works) I am forgetting anything to clean up / protect myself from accessing garbage memory, etc. I wasn't sure in my functions if I should delete or if they're automatically destroyed at the end of the function.

    Thank you for your time, in advance.

    Code:
    #include <iostream>
    #include <ctime>
    
    // Node prototype
    
    struct node
    {
    	int x;
    	node *next;
    };
    
    // Function prototypes
    
    node* NewNode(node *end);
    int TravList(node *root);
    
    // Code
    
    int main()
    {
    	srand(time(NULL));
    	
    	node *root, *link;
    	root = new node;
    	
    	root->next = 0;
    	root->x = 5;
    	
    	link = NewNode(root);
    	link = NewNode(link);
    	
    	TravList(root);
    	
    	std::cin.get();
    	return(0);
    }
    
    node* NewNode(node *root)
    {
    	node *link;
    	link = root;
    	
    	link->next = new node;
    	link = link->next;
    	link->next = 0;
    	link->x = (rand()%100 + 1);
    	
    	root->next = link;
    	
    	return(link);
    }
    
    int TravList(node *root)
    {
    	node *link;
    	link = root;
    	
    	while (link->next != 0)
    	{
    		std::cout << link->x << "\n";
    		link = link->next;
    	}
    	
    	std::cout << link->x;
    	
    	return(0);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    You should have some kind of DeleteList function that cleans up all that allocated memory, something similar to your TravList function that calls delete on the nodes in the list instead of performing output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok, something like..

    Code:
    int DelList(node *root)
    {
    	node *link, *temp;
    	link = root;
    	
    	while (link->next != 0)
    	{
    		temp = link;
    		link = link->next;
    		delete temp;
    		temp = 0;
    	}
    	
    	return(0);
    }
    ?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Something to that effect, yes. Although, you may want to change your loop condition to while(link). Since all nodes are dynamically allocated, they won't be automatically cleaned up when your program goes out of scope - so you have to do it yourself.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Thank ya. I wasn't sure how to check if all the nodes were being deleted, so I'll take your while() loop on faith.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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