这是释放单个链表的内存的C代码。它使用Visual C++ 2008进行编译,并且代码正常工作。关于编译器及其工作原理的问题
/* Program done, so free allocated memory */
current = head;
struct film * temp;
temp = current;
while (current != NULL)
{
temp = current->next;
free(current);
current = temp;
}
但我也遇到过(甚至在书)这样写的相同代码:
/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
如果我编译的代码与我的VC++ 2008,程序崩溃,因为我第一次释放电流,然后分配当前 - >下一个电流。但显然,如果我用其他编译器(例如,本书作者使用的编译器)编译此代码将会起作用。所以问题是,为什么这个代码是用特定的编译器工作编译的?是否因为编译器把指令放在二进制文件中,记住current-> next的地址,尽管我释放了current,而我的VC++却没有。我只想了解编译器是如何工作的。
哪些书是那些? – 2010-04-14 10:31:33
@尼尔,坏的。 – 2010-04-14 10:34:04
请告诉我们这本书,这样我们可以避免它,并建议反对它。 – 2010-04-14 10:37:19