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.
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); }
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..
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.