If you use a variable out of scope like that the results are undefined. Undefined means it may or may not "seem to work" depending on external factors. For example, allocating a large variable may keep the corruption to a part of the stack that isn't important. The thing is, test1 and test2 are wrong. Passing the variable in (as in test3) is valid.