#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