As I want to pass an uninitialized pointer to a function, it goes runtime error. but if I pass this pointer as a reference, it works OK. I cannot explain why...
class Body
{
};
void check(Body* b)
{
b = new Body();
}
void checkRef(Body* &b)
{
b = new Body();
}
int main001()
{
Body* b;
//check(b);// error: The variable 'b' is being used without being initialized. (in VS2010)
checkRef(b); // OK
return 0;
}
Whats the difference when b is passed to check and checkRef? I get the runtime error in VisualStudio2010. error:The variable 'b' is being used without being initialized.
EDIT: it was a VS2010 debug output. the "error" doesn't appear in release version
check results in a memory leak but should not be runtime error. Well the difference is CheckRef will result into a memory allocation whose pointer will be with b inside main. In other case, when check returns, b will still be dangling.