Thread: Enter and write out words into a tree of nodes

  1. #46
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Joe1903 View Post
    Ok I will read the chapter again now.
    Another question: I want to replace class 'string' with word <directly> as const char*.But I am not sure how.Could you please advice?
    Please can someone help me out with this question?

  2. #47
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I dunno, you figured out how to use strcpy - perhaps you should look at how that function is declared, and make addNode have the same kind of 'char-pointery-stringlike' interface.

    I thought you were meant to be learning C++. What you're asking is a backward step.
    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.

  3. #48
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    I dunno, you figured out how to use strcpy - perhaps you should look at how that function is declared, and make addNode have the same kind of 'char-pointery-stringlike' interface.

    I thought you were meant to be learning C++. What you're asking is a backward step.
    Yes meanwhile, I' ve read and have an idea. I will post it after I' ve done it.I would be very happy if you can have a look afterwards.
    Now I' ve done the try-catch-block. Is that OK so?

    Code:
    int main(int argc, char* argv[])
    {
            Tree tree;
            Tnode* root = 0;
            string word;
            try
            {
                    for(int i = 1; i < argc; i++)
                    {
                            word = string(argv[i]);
                            Tnode* node = tree.addNode(root, word);
                            if (!root)
                            {
                                    root = node;
                            }
                    }
                    cout << "Tree:" << endl;
                    tree.printTree(root);
                    cout << "Alphabetical:" << endl;
                    tree.alphabeticPrintTree(root);
                    tree.freeTree(root);
            }
            catch (exception& e)
            {
                    cout << "Standard exception: " << e.what() << endl;
            }
            return (0);
    }

  4. #49
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    In this try-catch-Block above, is it neccessary to check the return value of new (nullptr if no memory allocatable) or is it handled by exception class automatically?
    I need to ensure that there is no memory leak..

  5. #50
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Keep in mind, you usually only want to catch exceptions when you can a. handle it and b. actually want to do something about it.

    In the case of being out of memory, there's nothing you can do, really, except let the process die. Fortunately, operating systems are well-coded enough that process death releases the acquired resources anyway so you don't need to worry about a memory leak in the case of an exception. If you get an out-of-memory exception, just let your code die. Or catch the exception, print out a nice little error message about it and then let it die. There isn't much you can do if you need RAM and none is available.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. actions on tree with 4 nodes
    By Dave11 in forum C Programming
    Replies: 2
    Last Post: 05-09-2014, 12:07 PM
  2. Write/Read tree nodes into a binary file
    By frank1 in forum C Programming
    Replies: 2
    Last Post: 10-21-2013, 12:37 AM
  3. Issue with tree nodes
    By coffee_cups in forum C++ Programming
    Replies: 19
    Last Post: 06-14-2013, 08:16 PM
  4. total nodes in a tree
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 01-10-2010, 11:37 AM
  5. Binary Tree - sum of nodes
    By Tiffanie in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2008, 08:49 AM

Tags for this Thread