关于参考调用的问题?

发布于 2024-08-21 03:15:52 字数 896 浏览 10 评论 0原文

main() 使用参数 First Node 调用 Call_By_Test() 函数。 我已经在 Call_By_Test() 中释放了第一个节点,但是在 main() 中没有释放第一个节点地址,为什么?

typedef struct LinkList{
int data;
struct LinkList *next;
}mynode;

void Call_By_Test(mynode * first)
{
        free(first->next);
        first->next = (mynode *)NULL;
        free(first);
        first = (mynode *)NULL;
}
int main()
{
mynode *first;

first = (mynode *)malloc(sizeof(mynode));
first->data = 10;
first->next = (mynode *)NULL;

cout<<"\n first pointer value before free"<<first<<endl;

Call_By_Test(first);
// we freed first pointer in Call_By_Test(), it should be NULL
if(first != NULL)
        cout<< " I have freed first NODE in Call-By-Test(), but  why first node pointer has the value "<<first<<endl;


}

输出: 第一个指针值0x804b008 我已经在 Call-By-Test() 中释放了第一个节点,但是为什么第一个节点指针的值为 0x804b008

main() calls Call_By_Test() function with argument parameter First Node.
I have freed the First Node in Call_By_Test() but First node address not freed in main(), why ?.

typedef struct LinkList{
int data;
struct LinkList *next;
}mynode;

void Call_By_Test(mynode * first)
{
        free(first->next);
        first->next = (mynode *)NULL;
        free(first);
        first = (mynode *)NULL;
}
int main()
{
mynode *first;

first = (mynode *)malloc(sizeof(mynode));
first->data = 10;
first->next = (mynode *)NULL;

cout<<"\n first pointer value before free"<<first<<endl;

Call_By_Test(first);
// we freed first pointer in Call_By_Test(), it should be NULL
if(first != NULL)
        cout<< " I have freed first NODE in Call-By-Test(), but  why first node pointer has the value "<<first<<endl;


}

Output:
first pointer value 0x804b008
I have freed first NODE in Call-By-Test(), but why first node pointer has the value 0x804b008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅浅淡淡 2024-08-28 03:15:52

答案是您没有使用引用传递。您正在按值传递指针 - 这不是同一回事。这意味着您将看到指针引用的数据发生变化,但在 Call_By_Test 方法中更改 first 本身的值不会执行任何操作。

The answer is that you're not using pass-by-reference. You're passing a pointer by value - and that's not the same thing. That means you'll see changes in the data that the pointer refers to, but changing the value of first itself within the Call_By_Test method does nothing.

一梦浮鱼 2024-08-28 03:15:52

由于问题被标记为 c++,我将重构为:

void Call_By_Test( mynode *& first ) // rest of code remains the same

这传达了引用传递,而无需额外的取消引用。所有建议将指针传递给指针的解决方案 (void Call_By_Test( mynode ** first )) 都在指向指针变量的指针中使用按值传递语义。虽然您可以在 C++ 中执行此操作,但按引用传递更清晰。

Since the question is tagged c++, I would refactor to:

void Call_By_Test( mynode *& first ) // rest of code remains the same

That conveys the pass-by-reference without extra dereferences. All the solutions that propose passing a pointer to the pointer (void Call_By_Test( mynode ** first )) are using pass-by-value semantics in a pointer to the pointer variable. While you can do this in C++, pass-by-reference is clearer.

思慕 2024-08-28 03:15:52

在函数中,

void Call_By_Test(mynode * first)

first 实际上是函数的局部变量。更改它不会改变程序其余部分的状态。您需要一个对指针的引用或一个指向指针的指针:

void Call_By_Test(mynode ** first)
{
        free((*first)->next);
        (*first)->next = NULL;
        free(*first);
        *first = NULL;
}

并调用它:

Call_By_Test( & first );

In the function

void Call_By_Test(mynode * first)

first is effectively a local variable of the function. Changing it will not change the state of the rest of the program. You need a reference to a pointer or a pointer to a pointer:

void Call_By_Test(mynode ** first)
{
        free((*first)->next);
        (*first)->next = NULL;
        free(*first);
        *first = NULL;
}

and to call it:

Call_By_Test( & first );
霞映澄塘 2024-08-28 03:15:52

当您这样做时:

void Call_By_Test(mynode * first)

您首先复制,因此您可以处理位于第一个的内容,但您不能先更改地址,因为它是副本。

如果你想首先更改值,那么你应该有一个像这样的函数:

void Call_By_Test(mynode ** first)

或者

void Call_By_Test(mynode & first)

它允许你首先访问参数,就像它是原始变量一样(而不是主函数的副本)

When you do:

void Call_By_Test(mynode * first)

You copy first, so you can work on what is located in first but you can not change the address first since it is a copy.

If you want to change the value first then you should have a function like this:

void Call_By_Test(mynode ** first)

or

void Call_By_Test(mynode & first)

which allow you to access the argument first as if it was the original variable (and not a copy from the main function)

北城挽邺 2024-08-28 03:15:52

如果您释放一个指针(例如0x804b008),则不会更改指针本身的位。它仅仅允许C++库记录所指向地址处的内存不再使用,并且可以回收。在您的情况下,当您打印 0x804b008 时,地址 0x804b008 处的内存可供 C++ 库重用。

If you free a pointer (such as 0x804b008), that doesn't change the bits in the pointer itself. It merely allows the C++ library to record that the memory at the pointed-to address is no longer in use, and can be recycled. In your case, when you print 0x804b008,the memory at address 0x804b008 is available for reuse by the C++ library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文