【发布时间】:2010-02-15 13:50:34
【问题描述】:
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
【问题讨论】:
-
@SIVA:如果您对过去 6 个问题的某些答案感到满意,您应该接受它们(通过单击空心复选标记)。
-
您在
Call_By_Test中做了两件不同的事情:您调用free(ptr)和ptr=NULL。您希望这两者中的哪一个会影响 main() 中的first? -
我仍然想知道为什么大多数人从 C 子集开始学习 C++...
-
这不是 C++ 代码,这是带有
cout而不是printf的 C。
标签: c++ pass-by-reference argument-passing