0

Solution Below For almost a year, I thought I completely understood pointers, and now it's failing. I will post the entire file if it is even needed.

// Test Structure and Function
struct You {
    int x;
    int y;
    string str;
};

bool Show(You* showValue);


// Should (delete) in whatever way possible and update its address to the     "You* update" you sent
void Update(You* update, int n) {

    // Create a new "You"
    You* youTwo = new You();
    youTwo->x = 55;
    youTwo->y = 43;
    youTwo->str = "Twin";

    // Update?
    update = youTwo;

    return; 
};




bool Show(You* showValue) {
    cout << "Show:" << endl;
    cout << showValue->x << '\t';
    cout << showValue->y << '\t';
    cout << showValue->str << '\t'; 
    cout << endl << endl;
};



int main(int argc, char** argv) {

    // Original You
    You* currentYou = new You();
    currentYou->x = 1;
    currentYou->y = 2;
    currentYou->str = "You";

    // Update the current you to a new you
    Show(currentYou);   // works
    Update(currentYou, 5);  // no compile errors
    Show(currentYou); // shows initial values instead of the updated

    return 0;
};

The Update function is where the issue is. My intentions is to delete (or get rid) of the original. Replacing it with a new You() and be done with it.

1
  • 1
    Well, congrats to solving the problem. Meanwhile, if you keep the original problematic code there and provide the solution separately, it will be more useful to other readers. Commented Jul 1, 2015 at 1:25

2 Answers 2

3

You pass the pointer to You by value to void Update(You*,int). Thus update = youTwo; this has no effect on currentYou.

Change Update to void Update(You*& update, in n) { //... and read up on references.

Btw, you have a memory leak. You update the pointer, but you never deallocate the old currentYou nor the new currentYou. You should use "smart pointers" (explicitly shared_ptr<You>) to clean up everything behind you, without having to call delete every time yourself.

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

2 Comments

thanks, the memory leak I actually left, I was trying to rule that out of the issue. I will try your advise.
@EvanCarslake, you have to delete update; before you assign youTwo so that you delete the previous You. Other than that it should be fine (after formatting)
0
#include <iostream>
#include <string>
using namespace std;



// Test Structure and Function
    struct You {
    int x;
    int y;
    string str;
};


bool Show(You* showValue);



// Should (delete) in whatever way possible and update its address to the "You* update" you sent
void Update(You*& update, int n) {

// Clean Up
delete update;

// Create a new "You"
You* youTwo = new You();
youTwo->x = 55;
youTwo->y = 43;
youTwo->str = "Twin";

// Update address
update = youTwo;

return; 
};




bool Show(You* showValue) {
cout << "Show:" << endl;
cout << showValue->x << '\t';
cout << showValue->y << '\t';
cout << showValue->str << '\t'; 
cout << endl << endl;
};



int main(int argc, char** argv) {

// Original You
You* currentYou = new You();
currentYou->x = 1;
currentYou->y = 2;
currentYou->str = "You";

// Update the current you to a new you
Show(currentYou);
Update(currentYou, 5); 
Show(currentYou); 

delete currentYou;
return 0;
};

The memory leak is fixed (the delete's were added,) and the pointer is a pointer-ref now. Works perfect.

1 Comment

What if there's another reference to the old update when calling Update? You invalidate it completely, without anybody knowing

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.