5

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

6
  • Are you sure the first one gives you a runtime error, and not a compile-time error? What is the error message that you get? Commented May 5, 2011 at 10:18
  • 1
    Your life would be much easier if check() returned the pointer. Commented May 5, 2011 at 10:21
  • I am sure. in VS2010, @"The variable 'b' is being used without being initialized." Commented May 5, 2011 at 10:22
  • I don't think it should give any runtime error. 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. Commented May 5, 2011 at 10:23
  • @demaxSH: It's almost certainly not a runtime error, though it could be output from your debugger. A little screenshot would clear this up, but it doesn't really matter. Commented May 5, 2011 at 10:54

4 Answers 4

8

In order to be equivalent to the checkRef version, your check function should read:

void check(Body** b)
{
    *b = new Body();
}

and called as

check(&b);

If you don't pass the address, as you do in check(b), then you are passing the current value of the pointer b which is indeed uninitialised.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. It's important to remember that "uninitialised" is more than just "doesn't point to anything sensible", or "points to an arbitrary place in memory". Using it uninitialised is invalid (strictly, UB)... whatever "using" means.
3

Body* b does not set b to point to anything sensible. So it may point to some random location which could cause the earth to shift a bit.

Passing it to check(by value for the pointer) does not make it initialised in any way

void check(Body* b)
{
    b = new Body();
}

If you passed it by pointer to pointer, it should be okay

  void check(Body** b)
  {
     *b = new Body();
  }

And the call

check(&b);

Better done in C++ way with the reference example you give since that updates the referenced pointer value to point to the newly allocated Body

Comments

2

In Body * &b b is an alias of/reference to the b in main - so when you assign to it you modify the b in main. In Body* b b is a local copy of the b in main, you're modifying this local copy and not the b in main.

Your runtime error is likely due to using the uninitialized b in main.

EDIT: The actual error you get is an extra check-for-typical-problems mechanism embedded into your code by your compiler. It detects that you pass something uninitialized by value, which doesn't make sense, and therefore generates this error.

If you disable this behavior, the application will appear to function properly until you try to dereference the uninitialized b in main

3 Comments

@demaxSH: The error you get is because the compiler generates code to check for such problems - You're passing something uninitialized by value, and that doesn't make sense in any way so a compiler-generated check produces this error. If you built a "Release" build with such extra checks disabled, you wouldn't get this error, you'd see the symptoms of it later in the application when trying to use the uninitialized b
Yes I didn't get runtime errors when compiled in gcc. I didn't realize the "new" initializer will modify the pointer, well now I know
@demaxSH: it's not the new that modifies the pointer, it's the assignment to the pointer. Assignment will always modify the object being assigned; that is its purpose.
0

I don't know why you're seeing a run-time error; at most I would expect to receive a compile-time warning/error. But you can probably fix it by initialising b to NULL (but this won't fix your program; see below...)

The difference between the first and the second function call is that the first one will cause a memory-leak. You are passing the pointer by-value, so inside check() you are working with a copy of that pointer. You assign this copy, but this doesn't affect the value of the pointer in main(), so when you leave the function, there is no way to access the memory that was obtained by new.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.