should i use initialization list?

This is a discussion on should i use initialization list? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> template <typename Type> class LinkList { struct Node { Type item; Node *next; // Should i use ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    Thumbs up [Fixed] should i use initialization list?

    Code:
    #include <iostream>
    
    template <typename Type>
    class LinkList {
    	struct Node {
    		Type item;
    		Node *next;
    
    // Should i use initialization list here? 
    // Which is better? (safer? faster?)
    		Node(Type i, Node *n=0) {
    			item = i;
    			next = n;
    		}
    	} *root;
    	
    public:
    	LinkList() : root(0) {}
    
    	void add(Type item) {
    		if (root) {
    			Node *tmp = root;
    			while (tmp->next) tmp = tmp->next;
    			tmp->next = new Node(item);
    		} else {
    			root = new Node(item);
    		}
    	}
    
    	void print() {
    		std::cout << "List items: ";
    		for (Node *tmp = root; tmp; tmp = tmp->next) {
    			std::cout << tmp->item << " ";
    		}
    		std::cout << std::endl;
    	}	
    };
    
    int main()
    {
    	LinkList<int> list;
    	int choice, item;
    	while (true) {
    		std::cout << "1) Add\n2) Print\n0) Quit\n? ";
    		std::cin >> choice;
    		if (choice == 1) {
    			std::cin >> item;
    			list.add(item);
    		} else if (choice == 2) {
    			list.print();
    		} else if (choice == 0) {
    			break;
    		}
    	}
    	
    	return 0;
    }
    Last edited by manav; 05-16-2008 at 01:04 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Should i use initialization list here?
    Yes.

    Which is better? (safer? faster?)
    Using an initialization list is potentially faster since it avoids creating the member object and the assigning to it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by manav View Post
    // Should i use initialization list here?
    // Which is better? (safer? faster?)
    Add "More correct" to that list.

    If you don't list a member in the initialization list, it will be default-constructed. Then in the constructor body, it will be assigned a new value through its assignment operator. For one thing, this is inefficient, for another, it is simply wrong if the member has no default constructor or if its constructor does something different from what the assignment operator does.

    For basic data types it doesn't matter as much, but the rule is to use the initialization list to initialize members unless there is some reason why that is completely impossible.

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks laserlight, brewbuck.
    i understand now.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    this is ~destructor for the LinkList class:
    Code:
    ~LinkList() {
    		Node *tmp = root;
    		while (tmp) {
    			delete tmp;
    			tmp = tmp->next;
    		}
    	}
    is it working correctly? i mean releasing the memory properly?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by manav View Post
    is it working correctly? i mean releasing the memory properly?
    It's wrong. You're accessing an object after it has been destructed.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by brewbuck View Post
    It's wrong. You're accessing an object after it has been destructed.
    sorry, i am confused, how is it wrong?

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by manav View Post
    sorry, i am confused, how is it wrong?
    You delete tmp, and then you access tmp->next. You shouldn't be accessing an object which doesn't exist.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    sorry i skipped that one in a hurry, after your post i rechecked my code, strange thing is that there was no runtime error during 3 test runs.

    now corrected:
    Code:
    ~LinkList() {
    		while (root) {
    			Node *tmp = root->next;
    			delete root;
    			root = tmp;
    		}
    	}
    is it ok now?

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by manav View Post
    sorry i skipped that one in a hurry, after your post i rechecked my code, strange thing is that there was no runtime error during 3 test runs.
    That sort of error is unlikely to cause a runtime fault... at the time.

    In a bigger program, errors like this eventually lead to instability and a crash.

    Code:
    ~LinkList() {
    		while (root) {
    			Node *tmp = root->next;
    			delete root;
    			root = tmp;
    		}
    	}
    is it ok now?
    Looks good now.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if your Node is a class which has a destructor like this:
    Code:
    ~Node() 
    {
       next = 0;
    }
    then you would only delete the first item...
    Or:
    Code:
    ~Node() 
    {
       next = reinterpret_cast<Node *>(-1);
    }
    would almost certainly make it crash.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks Mats.
    why would that happen is not clear to me, still, then what to do so that the LinkList is properly destroyed and all memory is freed?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    thanks Mats.
    why would that happen is not clear to me, still, then what to do so that the LinkList is properly destroyed and all memory is freed?
    I am talking about in your broken case where you access the node after it's been deleted. Try it if you like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by matsp View Post
    I am talking about in your broken case where you access the node after it's been deleted. Try it if you like.
    thanks again.
    actually adding a destructor to Node struct would have produced run time error, and, would have made me catch this earlier, i think my Node struct must have a destructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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