#include <stdio.h>
#include <stdlib.h>
#include <string.h>'
typedef struct NodeClass {
char lineThatContainsWord[100];
int lineNumber;
struct NodeClass *next;
} Node;
int main(void) {
Node *head;
head = malloc(sizeof(Node));
Node *tail = NULL;
head->next = tail; /* sets head equal to NULL */
strcpy(head->lineThatContainsWord,"hello");
head->lineNumber = 5;
free(head);
head->next = malloc(sizeof(Node));
head->next->next = NULL;
strcpy(head->next->lineThatContainsWord,"hello2");
head->next->lineNumber = 10;
tail = head->next;
free(tail);
printf(tail->lineThatContainsWord);
printf("\nlineNumber is %d",tail->lineNumber);
return 0;
}
我假設通過設置tail = head-> next,它會打印head-> next節點的值。但是,此印刷在LinkedList中使用free()和內存分配C
hello2
lineNumber is 0
爲什麼只有lineThatContainsWord更新?爲什麼lineNumber不是?
我的任務要求我釋放的變量,所以我釋放他們後,我印製和它的工作。但是,我讀到釋放變量只是釋放它們指向的數據。如果我在訪問數據後發佈數據,完全釋放的意義是什麼? – csDS
完成使用後可以釋放它,以便內存可以用於其他內容。 – Barmar
程序員永遠不會釋放他們的結構對於程序員來說是不切實際的嗎?這會使他們的內存分配效率低下,對嗎? – csDS