0

I recently wrote a MyStack class which would work just like STL stack. Now i decided to write MyStack struct, which I will work with using functions instead of methods

struct Stack
{
    int _data;
    Stack* _next;

}*stack;

I have Erase function to delete all the elements and null the first element.

void Erase(Stack* stack)

{
    Stack* cur;
    while (stack != nullptr)
    {
        cur = stack;
        stack = stack->_next;
        delete cur;
    }
}

The problem is that only the local stack pointer is being nulled, not the one I pass pass into the function, so if I call the function again, it will run the loop and trigger a breakpoint. I decided to rewrite the function and after an hour I came up with a solution that actually nulls both local and passed pointers

void Erase(Stack** pStack)
{
    Stack* cur;
    while (*pStack != nullptr)
    {
        cur = *pStack;
        *pStack = (*pStack)->_next;
        delete cur;
    }
}

Why does it work? Why is dereferenced pointer to pointer works differently from just a pointer?

jxh
  • 69,070
  • 8
  • 110
  • 193
Miroigrin
  • 11
  • 1
  • 1
    A pointer is itself a variable. You can't affect the caller's variable in the function unless you have a pointer to their variable. – jxh Mar 25 '22 at 18:32
  • 1
    Function parameters are passed by value, so in this version of the function `void Erase(Stack* stack)` the parameter `stack` is a copy of the original pointer. It points to the same data so you can modify that all you like, but changes to the value of the copy are lost when the function returns. Neither function explicitly sets anything to NULL. – Retired Ninja Mar 25 '22 at 18:33
  • @RetiredNinja Unless `_next` is NULL. – jxh Mar 25 '22 at 18:35
  • "Why is dereferenced pointer to pointer works differently from just a pointer?" Because when you pass a pointer-to-pointer, it points *at the pointer* in the caller. Therefore, when you write *to the dereferenced* pointer-to-pointer, you overwrite the caller's pointer, rather than a *copy* made when the function is called. – Karl Knechtel Mar 25 '22 at 18:37
  • This is a pass by reference in C question. There are many candidate duplicates. – jxh Mar 25 '22 at 18:38
  • 1
    It makes sense now. If I pass a pointer by reference instead of a pointer, the first function should now null the original pointer, right? – Miroigrin Mar 25 '22 at 18:40
  • @KarlKnechtel Yes it does! Now I get it. I don't know how I didn't find this page myself. – Miroigrin Mar 25 '22 at 18:45
  • "I don't know how I didn't find this page myself." Well, what steps [did you try](https://meta.stackoverflow.com/questions/261592) in order to find it (or something similar)? What I tried was putting `[c] pass by pointer` into the site search. Another popular approach is to use a search engine, perhaps specifying `site:stackoverflow.com`. – Karl Knechtel Mar 25 '22 at 18:46
  • 1
    @KarlKnechtel What may have thrown the asker is that the language is C++, but the question is actually rooted in how C does things. In C++, the OP could have used a reference parameter. – jxh Mar 25 '22 at 18:56
  • Oh, I actually glossed over the fact that it was C++ code while looking for a duplicate (even though there are obvious markers) exactly because normally one would pass by reference in C++ :) – Karl Knechtel Mar 25 '22 at 19:00
  • @KarlKnechtel I think I should've spend more time googling it, instead, I tried to do everything myself – Miroigrin Mar 25 '22 at 19:01
  • @KarlKnechtel we learn C first in my University, not C++. But I could have guessed that if using pointer to pointer solves the problem, reference should have also done the job. I just don't use references that often to get used to them – Miroigrin Mar 25 '22 at 19:07
  • 1
    Realize that a reference to a pointer is a better interface than a pointer to a pointer. With a pointer to a pointer it is legal to pass in `nullptr`, which I bet your modified implementation would crash. If you changed it to a reference to a pointer, your original code would work properly. – franji1 Mar 25 '22 at 19:29

0 Answers0