我在C. 讀一本非常流行的斯坦福文檔*有關鏈表這裏面一個簡單的函數,建立具有三個成員的簡單LL。我沒有得到的唯一的東西,我感到困惑的是,它說它將head
指針存儲在「本地堆棧變量」中。但是,head
指針被分配在堆中!請看看代碼,並幫助我理解爲什麼它是一個本地堆棧變量?堆棧變量混淆使用C
/*
Build the list {1, 2, 3} in the heap and store
its head pointer in a local stack variable.
Returns the head pointer to the caller.
*/
struct node* BuildOneTwoThree() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1; // setup first node
head->next = second; // note: pointer assignment rule
second->data = 2; // setup second node
second->next = third;
third->data = 3; // setup third link
third->next = NULL;
// At this point, the linked list referenced by "head"
// matches the list in the drawing.
return head;
}
該文件提供瞭如何繪製鏈接列表在內存中的樣子。我不明白爲什麼head
指針繪製在堆棧區域!
*:http://cslibrary.stanford.edu/103/LinkedListBasics.pdf,頁5和6
你在混淆「頭部指針」和「頭部指針指向的東西」。 – immibis
請您詳細說明一下嗎?代碼是說'頭'指針是本地的功能。哪個局部變量?所有變量('head','second'和'third')堆在一起 –
這個頭指針**變量**,*就像那個函數中的所有其他變量*一樣,都是自動的(在你的白話中,它們活着這個想法就是這樣,在範圍退出時,變量'head'到期並且不再存在,因此它的值被丟失了。那麼,如果你做了*會不會將'head'中的值作爲函數結果返回給調用者,然後調用者將其存儲起來?除非有其他人保留這個值(就像你通過'返回'給調用者那樣),否則它永遠丟失了,並且有了它,任何機會都可以「讓」你的鏈表的初始節點。 – WhozCraig