0
**So recently i came up with a question in an interview it was based on pointers**
void fun(int *p){
    int q = 10;
    p = &q;
}
int main() {
    int r = 20;
    int *p = &r;
    fun(p);
     cout<<*p<<endl;
}

*my question is (1)Justify the result w.r.t the memory allocation like how this is happening?; (2) how to pass this pointer variable by refrence (i wrote *&p in the parameter of void fun()) when i did it i observed that the p is printing a garbage value ,Now first i thought may be fun has different memory allocation and when it takes the address of q from fun function it address changes but that address in main function is pointing to some garbage value ,Am i right and please explain?

  • 2
    Does this answer your question? [Passing references to pointers in C++](https://stackoverflow.com/questions/823426/passing-references-to-pointers-in-c) – prehistoricpenguin Aug 18 '21 at 03:42
  • 1
    @Ch3steR - Wrong. The assignment to `p` in `fun()` has no visible effect in `main()`. Since the value of `p` in `main()` is initialised to the address of `r`, and that doesn't change, the output is the value of `r`. i.e. 20. If `fun()` accepted a pointer by reference, then the code would have undefined behaviour like you describe. – Peter Aug 18 '21 at 03:45
  • Yes can you explain how that happens and why that happens,I just wanted a clarification from someone who is really comfortable with this ,thank you so much for this :) – Saurav kumar Aug 18 '21 at 03:46
  • @Peter Yes true, That's what I meant to say. I assumed reference by a pointer in OP's code. Deleting the comment as its misleading. – Ch3steR Aug 18 '21 at 03:47
  • Getting a garbage result after `*&p` is almost to be expected. `p` is pointed at `q`, but `q` goes out of scope at the end of `fun` and is no longer valid when `*p` is printed in `main`. Accessing an expired variable is what's called [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub) and you can't say exactly what'll happen when the behaviour isn't defined. You can get garbage. You can get 10. You can get the utterly fantastical like a rain of unicorns. But you'll probably get garbage or 10. – user4581301 Aug 18 '21 at 04:00

1 Answers1

1
void fun(int *&p) {
    int q = 10;
               // here the object q began its existence
    p = &q;
               // here you pass the address of that object *out*
               // of the function, to the caller

               // here the object q ceases to exist
               // question: does anyone have now-invalid pointers that
               //    pointed to that object?
}

You'll of course immediately see that yes, the caller of fun has a pointer that doesn't point to a valid object (objects that don't exist are by definition invalid). This is undefined behavior. Anything can happen.

Whatever you observe is just as good as what anyone else would observe :) I can make this code pretend to work, or pretend fail on almost any compiler - it's only a matter of arranging things in a certain way. That doesn't make the code any more valid, of course, and the notion of "working" or "failing" is meaningless anyway: the code is invalid, so as far as C++ is concerned, the discussion about the effects is invalid as well :)

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313