关于参考调用的问题?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
答案是您没有使用引用传递。您正在按值传递指针 - 这不是同一回事。这意味着您将看到指针引用的数据发生变化,但在 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.由于问题被标记为 c++,我将重构为:
这传达了引用传递,而无需额外的取消引用。所有建议将指针传递给指针的解决方案 (
void Call_By_Test( mynode ** first )
) 都在指向指针变量的指针中使用按值传递语义。虽然您可以在 C++ 中执行此操作,但按引用传递更清晰。Since the question is tagged c++, I would refactor to:
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.在函数中,
first 实际上是函数的局部变量。更改它不会改变程序其余部分的状态。您需要一个对指针的引用或一个指向指针的指针:
并调用它:
In the function
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:
and to call it:
当您这样做时:
您首先复制,因此您可以处理位于第一个的内容,但您不能先更改地址,因为它是副本。
如果你想首先更改值,那么你应该有一个像这样的函数:
或者
它允许你首先访问参数,就像它是原始变量一样(而不是主函数的副本)
When you do:
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:
or
which allow you to access the argument first as if it was the original variable (and not a copy from the main function)
如果您
释放
一个指针(例如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 print0x804b008
,the memory at address0x804b008
is available for reuse by the C++ library.