0

I’m working on a beginner(!) exercise.

I am comfortable passing basic variables and also using &variable parameters so I can make changes to the variable that are not destroyed when returning. But am still learning pointers. I am working on the basic Mutant Bunny exercise (linked list practice).

In it I create a linked list by declaring Class Bunny. I set it up as you expect with a data section and a ‘next’ pointer for set up the linkage.

struct Bunny {
    string name;
    int age;
    // more variables here
    Bunny* next;
};

Everything works great when I call function to do things like create Bunnies using the function:

Bunny* add_node ( Bunny* in_root ){}

This sets up the node and returns it just like I want. I can also do things like call a function to modify the Bunny class like aging the bunnies.

void advanceAge ( Bunny* in_root ){}

I pass in the head and then I can modify the bunnies in the called function and it stays modified even when it goes back to main. For example I can use:

in_root->age ++; 

in the called function and when I return to ‘main’ it is still changed. Basically I can use -> in any called function and it makes the change permanently. I think because the pointer is dereferenced(?) by the -> but still getting my head around it...

So far so good.

The problem comes up when I want call a function to delete the list. (Nuclear option… no more bunnies)

I can delete all the nodes in the called function… but it does not change the Bunny in ‘main’. For example… this does not permanently remove the node.

void DeathCheck(Bunny* in_root){
    Bunny* prev_ptr;
    prev_ptr = in_root;
    if (prev_ptr == NULL){
        cout << "No list to check age." << endl; return;
    } else {        
prev_ptr = NULL;   // <- what could I code to have this stick?      return;} 
// rest of DeathCheck

I’m curious if there is a way to set the node to NULL in the called function and have it stick?

Marc
  • 11
  • You need to provide more details. How exactly add_node() works. And where exactly, and how, is the entire list of bunnies is stored. There's a reasonable chance that you may not be doing what you think you're doing in add_node(). – Sam Varshavchik Sep 01 '16 at 23:43

2 Answers2

1

Since you're passing in_root by value, there's no way for it to modify the caller's variable. You could pass it by reference.

void DeathCheck(Bunny* &in_root) {
    Bunny *prev_ptr = in_root;
    ...
    in_root = nullptr;
    return;
}
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

Currently, in DeathCheck(Bunny* in_root), there is no way that in_root can be changed, only the object it is pointing to can be changed. (See pass by reference and value with pointers). Based on this, you need to change the parameter to pass-by reference, eg by changing the signature of your function to this:

DeathCheck(Bunny* &in_root) 
{
    //...
}

This passes the Bunny by reference, meaning that it can now be reassigned to without a copy.

Community
  • 1
  • 1
Arnav Borborah
  • 9,956
  • 4
  • 32
  • 69