I'm working on a problem on topcoder, and below is the code I've got so far.
To illustrate the problem, compile the code with "g++ -std=c++0x main.cc" and run the program. You will see "segmentation fault".
Now, uncomment the line printing "hello"(in function minSumlength).
You see everything behaves correctly.
Also, you don't need a ';' after '}'. Probably not an error, but it reduces readability [in my opinion], as you normally only see '};' after a class declaration.
@coder777:
I'm afraid that's not the problem.
In fact, the programming problem had a complex background and it's not possible to explain my solution clearly in a few sentences without drawing some diagrams. I might have to do this and post the solution and its background if no one is able to help me find the problem from a purely technical perspective.
In fact, it is guaranteed that when line 124 or 164 gets executed, they will have valid values. If you run my code, you'll see the output proves what I say. The segmentation fault is not due to an improper operation(-> operator in this case) on a NULL pointer; instead, it is because of the function showTree(). As you can tell, I'm doing an inorder traversal to cout the state of each node in the tree using recursion; The real problem is, I am in a dead loop of recursion(in showTree()) and finally my program appears to run out of stack and access some unauthorized memory. You may verify this by taking a look at the output. I print out the address of left children when doing inorder traversal, so you can see a repeated pattern of addresses. (the loop is in the left branch)
The weird thing is:
if we uncomment line 189, the program prints correct results! The addresses(and other info) printed out are EXACTLY correct. More importantly, we don't see a repeated pattern of addresses! This is incredible, because cout something does not change the structure of the tree; if the tree has a loop between two treenodes, the loop structure will NOT disappear simply because I cout hello or hi(it worked with hi as well)!
If this solves your problem, I'll *enlighten* (hehe, couldn't stop myself from writing this) you as to why your program didn't crashed after you uncommented the "hello"
In fact, it is guaranteed that when line 124 or 164 gets executed, they will have valid values.
No, it's not. It doesn't take much to see it, either.
When you reach line 113 for the first time, node is not initialized and contains junk.
Because this junk is unlikely to evaluate to NULL, pare gets a new value. Then you use the junk in node, resulting in undefined behavior. Be enlightened. These are the types of hard-to-track down problems you get with wild pointers, if you're unlucky enough not to crash.
@Pravesh & cire:
You are the men!
I double checked with c++ standard, and it doesn't specify the default value of a pointer, so it must be some random value. Well, I had the mistaken impression that pointers have NULL as their default values. And that's my downfall.
I kinda see why. When the stack of formTree() gets destroyed and then created again, the node and pare have the old values. Now with my solution diagram, it makes perfect sense why I have a dead loop in the tree.
An interesting question will be, why does my situation change when I decide to cout some random stuff? Purely luck?
Well, thank you guys very much! I'm indeed *enlightened*, and it feels soooo great! :)