I'm writing a program with binary tree. The program runs perfectly ok, no syntax errors, no logical error, but when it terminates, i got this error msg
"Run - Time Check Failure #2 - stack around the variable 'myTree' was corrupted".
Can anyone pls tell me what happened?
stack corruption is usually caused by you stepping out of bound in an array somewhere.
Since it's stack corruption and not heap corruption, this means you're likely stepping out of bounds in an array that's allocated on the stack (local to a specific function)
Since stack corruption is usually detected when a function exits, that means the culprit is likely in whetever function is running when the program gives you that message. Since you say you get it when the program closes, that tells me that main() is the function with the problem.
Post your main() in here so we can see it. Maybe we'll be able to spot the problem.
@archaic,
... and is one of them nested within the other one?
Lol I doubt it; that wouldn't compile.
@Disch,
Given that the stack corruption occurs at the end of the program, wouldn't that imply the destructor was at fault? If you had a buffer overflow or an off-by-one error then surely the program would crash when that overflow or error occurred. So the corruption likely occurs when the destructor is called. But his destructor is empty which is confusing.
how large are user and pass? I don't even see them defined anywhere.
Given that the stack corruption occurs at the end of the program, wouldn't that imply the destructor was at fault?
No.
The corruption occurs much earlier (pretty much anywhere in your program is suspect). The program is just telling you about it at the end of the function because that's when it unwinds the stack and can actually look for the corruption.
That's probably why then. It's an off-by-one error -- the loop that Disch indicated is iterating one too many times. Change the "<=" to "<" (if it's <= then you're trying to dereference pass[5] which doesn't exist (remember that arrays count from 0, so the last element in pass is pass[4])).