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

  1. #31
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    Post your latest code for addNode
    Code:
    ...
    Tnode* Tree::addNode(Tnode* root, string word)
    {
            Tnode* node;
            if (!root)
            {
                    node = new Tnode;
                    size_t copyLen = std::min(sizeof(node->word)-1, word.size());
                    strncpy(node->word, word.c_str(), word.size()-1);
                    node->word[copyLen] = '\0';
                    node->left = 0;
                    node->right = 0;
                    node->count = 1;
                    return (node);
            }
    
            int cmp = strcmp(word.c_str(), root->word);
            if (cmp < 0)
            {
                    node = addNode(root->left, word);
                    if(!root->left)
                    {
                            root->left = node;
                    }
            }
            else if (cmp > 0)
            {
                    node = addNode(root->right, word);
                    if(!root->right)
                    {
                            root->right = node;
                    }
            }
            else
            {
                    root->count++;
            }
            return (node);
    }
    
    ...

  2. #32
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,753
    > strncpy(node->word, word.c_str(), word.size()-1);
    You really need to learn how to copy/paste better.

    Seriously.
    strncpy(node->word, word.c_str(), copyLen);
    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. #33
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    > strncpy(node->word, word.c_str(), word.size()-1);
    You really need to learn how to copy/paste better.

    Seriously.
    strncpy(node->word, word.c_str(), copyLen);
    I am very sorry. Please sorry.My fault.

  4. #34
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    > strncpy(node->word, word.c_str(), word.size()-1);
    You really need to learn how to copy/paste better.

    Seriously.
    strncpy(node->word, word.c_str(), copyLen);
    It works now, thank you very much. May I ask you a last question regarding this code?

  5. #35
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    > strncpy(node->word, word.c_str(), word.size()-1);
    You really need to learn how to copy/paste better.

    Seriously.
    strncpy(node->word, word.c_str(), copyLen);
    I have implemented following to get an output if new allocation is not possible:

    Code:
    ...
    Tnode* Tree::addNode(Tnode* root, string word)
    {
            Tnode* node;
            if (!root)
            {
                    node = new(nothrow) Tnode;
                    if(node == 0)
                    {
                            cout << "Allocation returned nullptr\n" << endl;
                            exit(1);
                    }
                    size_t copyLen = min(sizeof(node->word)-1, word.size());
                    strncpy(node->word, word.c_str(), copyLen);
                    node->word[copyLen] = '\0';
                    node->left = 0;
                    node->right = 0;
                    node->count = 1;
                    return (node);
    ...
    Is that correct? And how can I insert more info in it like how much memory should be requested or for which object of which class was it requested.. Something like that.

  6. #36
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    I will also replace class 'string' with word <directly> as "const char*", but I am not sure how.Could someone advise please?

  7. #37
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Joe1903 View Post
    I have implemented following to get an output if new allocation is not possible:

    Code:
    ...
    Tnode* Tree::addNode(Tnode* root, string word)
    {
            Tnode* node;
            if (!root)
            {
                    node = new(nothrow) Tnode;
                    if(node == 0)
                    {
                            cout << "Allocation returned nullptr\n" << endl;
                            exit(1);
                    }
                    size_t copyLen = min(sizeof(node->word)-1, word.size());
                    strncpy(node->word, word.c_str(), copyLen);
                    node->word[copyLen] = '\0';
                    node->left = 0;
                    node->right = 0;
                    node->count = 1;
                    return (node);
    ...
    Is that correct? And how can I insert more info in it like how much memory should be requested or for which object of which class was it requested.. Something like that.
    Can anyone advise?

  8. #38
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,753
    What do you want to know?

    Are you suggesting that if you have a very long string that you spread it over several nodes?

    It's certainly possible, but you need to add further information to indicate that any particular node holds a complete string or a fragment.

    Also, calling exit is brutal.
    Pass NULL back to the caller, so a more appropriate error handling approach can be attempted.
    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.

  9. #39
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    What do you want to know?

    Are you suggesting that if you have a very long string that you spread it over several nodes?

    It's certainly possible, but you need to add further information to indicate that any particular node holds a complete string or a fragment.

    Also, calling exit is brutal.
    Pass NULL back to the caller, so a more appropriate error handling approach can be attempted.
    What I actually want to do is if there is no memory available for new[] it should write out a message like there is no space available with some additional info like e.g. how much memory is available currently.

  10. #40
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,753
    To be honest, in a C++ program, you shouldn't be using the (nothrow) unless you really have a good plan B to deal with the out of memory.
    Even if you do have a good plan B at this point in the program, using the C++ try/catch mechanism is the way to deal with it.

    Otherwise, let the system propagate std::bad_alloc to a point where the program can deal with it sensibly.

    For example, if you were writing a text editor, your user would be royally p***ed if the program just aborted because some edit operation failed. In this case, the exception needs to be caught before the whole program exits, and some effort needs to be made to save the current work to a recovery file.

    There is no good (that is portable) way to determine how much memory is available to a program. It might not even be a static value. For example, the more programs a system has running, the less real + swap memory there is to go around. Also, a single large block might fail where a greater amount of smaller blocks might succeed.
    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.

  11. #41
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    To be honest, in a C++ program, you shouldn't be using the (nothrow) unless you really have a good plan B to deal with the out of memory.
    Even if you do have a good plan B at this point in the program, using the C++ try/catch mechanism is the way to deal with it.

    Otherwise, let the system propagate std::bad_alloc to a point where the program can deal with it sensibly.

    For example, if you were writing a text editor, your user would be royally p***ed if the program just aborted because some edit operation failed. In this case, the exception needs to be caught before the whole program exits, and some effort needs to be made to save the current work to a recovery file.

    There is no good (that is portable) way to determine how much memory is available to a program. It might not even be a static value. For example, the more programs a system has running, the less real + swap memory there is to go around. Also, a single large block might fail where a greater amount of smaller blocks might succeed.
    Could you please give me an example with try-catch?

  12. #42
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,753
    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.

  13. #43
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    Is that correct?

    Code:
    #include <exception>
    ... 
    Tnode* Tree::addNode(Tnode* root, string word)
    { 
       Tnode* node; if (!root)
       { 
          try
          {
             node = new Tnode; 
          }
          catch (exceptions& e)
          {
             cout << "Standard exception: " << e.what() << endl;
          } 
    
          size_t copyLen = min(sizeof(node->word)-1, word.size()); 
          strncpy(node->word, word.c_str(), copyLen);         
          node->word[copyLen] = '\0'; 
          node->left = 0; 
          node->right = 0; 
          node->count = 1; 
          return (node);
     ...
    Last edited by Joe1903; 11-20-2016 at 08:10 AM.

  14. #44
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,753
    Well no, because you carry on and do what you were going to do anyway, having caught and subsequently ignored the meaning of the exception.

    The exception handler happens in main, where it says "your list is full, what do you want to do now".
    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.

  15. #45
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    Well no, because you carry on and do what you were going to do anyway, having caught and subsequently ignored the meaning of the exception.

    The exception handler happens in main, where it says "your list is full, what do you want to do now".
    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?

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