C++ C++;传递给函数时出现指针问题

C++ C++;传递给函数时出现指针问题,c++,pointers,C++,Pointers,下面的解决方案近一年来,我一直以为自己完全理解指针,但现在失败了。如果需要的话,我会发布整个文件 // 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* up

下面的解决方案近一年来,我一直以为自己完全理解指针,但现在失败了。如果需要的话,我会发布整个文件

// 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;
};
//测试结构和功能
构造你{
int x;
int-y;
字符串str;
};
bool Show(你*showValue);
//应以任何可能的方式(删除)并将其地址更新为您发送的“You*update”
无效更新(您*更新,int n){
//创建一个新的“你”
You*youTwo=新的You();
youTwo->x=55;
youTwo->y=43;
youTwo->str=“Twin”;
//更新?
更新=youTwo;
返回;
};
bool Show(你*showValue){

您可以通过值将指向
You
的指针传递给
void Update(You*,int)
。因此
Update=youTwo;
这对
currentYou
没有影响

Update
更改为
void Update(您*&Update,在n中){/…
并阅读参考资料


顺便说一句,您有内存泄漏。您更新了指针,但从未解除分配旧的
currentYou
或新的
currentYou
。您应该使用“智能指针”(显式
shared\u ptr
)清理你身后的一切,而不必每次自己调用
delete

你通过值将指向
you
的指针传递给
void Update(you*,int)
。因此
Update=youTwo;
这对
currentYou
没有影响

#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;
};
Update
更改为
void Update(您*&Update,在n中){/…
并阅读参考资料


顺便说一句,您有内存泄漏。您更新了指针,但从未解除分配旧的
currentYou
或新的
currentYou
。您应该使用“智能指针”(显式
shared\u ptr
)清理你身后的一切,而不必每次自己调用
delete

你通过值将指向
you
的指针传递给
void Update(you*,int)
。因此
Update=youTwo;
这对
currentYou
没有影响

#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;
};
Update
更改为
void Update(您*&Update,在n中){/…
并阅读参考资料


顺便说一句,您有内存泄漏。您更新了指针,但从未解除分配旧的
currentYou
或新的
currentYou
。您应该使用“智能指针”(显式
shared\u ptr
)清理你身后的一切,而不必每次自己调用
delete

你通过值将指向
you
的指针传递给
void Update(you*,int)
。因此
Update=youTwo;
这对
currentYou
没有影响

#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;
};
Update
更改为
void Update(您*&Update,在n中){/…
并阅读参考资料

顺便说一句,你有内存泄漏。你更新了指针,但你从未释放旧的
currentYou
或新的
currentYou
。你应该使用“智能指针”(显式
shared\u ptr
)来清理你身后的一切,而不必每次自己调用
delete

\35;
#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;
};
#包括 使用名称空间std; //测试结构与功能 构造你{ int x; int-y; 字符串str; }; bool Show(你*showValue); //应以任何可能的方式(删除)并将其地址更新为您发送的“You*update” 无效更新(您*&更新,int n){ //清理 删除更新; //创建一个新的“你” You*youTwo=新的You(); youTwo->x=55; youTwo->y=43; youTwo->str=“Twin”; //更新地址 更新=youTwo; 返回; }; bool Show(你*showValue){ 不能包含 #包括 使用名称空间std; //测试结构与功能 构造你{ int x; int-y; 字符串str; }; bool Show(你*showValue); //应以任何可能的方式(删除)并将其地址更新为您发送的“You*update” 无效更新(您*&更新,int n){ //清理 删除更新; //创建一个新的“你” You*youTwo=新的You(); youTwo->x=55; youTwo->y=43; youTwo->str=“Twin”; //更新地址 更新=youTwo; 返回; }; bool Show(你*showValue){ 不能包含 #包括 使用名称空间std; //测试结构与功能 构造你{ int x; int-y; 字符串str; }; bool Show(你*showValue); //应以任何可能的方式(删除)并将其地址更新为您发送的“You*update” 无效更新(您*&更新,int n){ //清理 删除更新; //创建一个新的“你” You*youTwo=新的You(); youTwo->x=55; youTwo->y=43; youTwo->str=“Twin”; //更新地址 更新=youTwo; 返回; }; bool Show(你*showValue){ 不能包含 #包括 使用名称空间std; //测试结构与功能 构造你{ int x; int-y; 字符串str; }; bool Show(你*showValue); //应以任何可能的方式(删除)并将其地址更新为您发送的“You*update” 无效更新(您*&更新,int n){ //清理 删除更新; //创建一个新的“你” You*youTwo=新的You(); youTwo->x=55; youTwo->y=43; youTwo->str=“Twin”; //更新地址 更新=youTwo; 返回; }; bool Show(你*showValue){
谢谢,我实际上留下了内存泄漏,我正试图排除这个问题。我会试试你的建议。@EvanCarslake,你必须
删除更新;
在分配
youTwo
之前,这样你就可以删除以前的
you
。除此之外,(格式化后)应该没问题谢谢,实际上我留下的内存泄漏,我正试图排除这个问题。我会试试你的建议。@EvanCarslake,你必须
删除更新;
然后再分配
youTwo
,这样你就可以删除以前的
。除此之外,它应该可以(格式化后)谢谢,我实际上留下的内存泄漏,我正试图排除这个问题。我会试试你的建议。@EvanCarslake,你必须
删除更新;
然后再分配
你两个
,这样你就可以删除p