[英]Printing Linked List leads to strings with garbage values (C)
我正在从 a.txt 文件中读取一个链表来填充以下节点:
typedef struct node {
int data;
char* line;
struct node* next;
} NODE;
该代码工作正常并将文件读入链接列表。 我在初始化每个节点时打印出字符串。 但是,每当我调用printList()
function 时, line
字符串始终是垃圾值。 样品运行:
./a.out linkedlisttext.txt
data in readFile(): 4
string in readFile(): Hello world
address in readFile(): 0x1326067a0
data in readFile(): 28
string in readFile(): What is up world???
address in readFile(): 0x1326067c0
data in readFile(): 2124
string in readFile(): your mother
address in readFile(): 0x1326067e0
data in readFile(): 85
string in readFile(): more data
address in readFile(): 0x132606800
data in readFile(): 9421
string in readFile(): just a little more data
address in readFile(): 0x132606820
data in readFile(): 992
string in readFile(): are we almost there?
address in readFile(): 0x132606840
data in readFile(): 301
string in readFile(): we did it!
address in readFile(): 0x132606860
DATA: 4 | STRING �g`2
address in printList(): 0x1326067a0
DATA: 28 | STRING �g`2
address in printList(): 0x1326067c0
DATA: 2124 | STRING �g`2
address in printList(): 0x1326067e0
DATA: 85 | STRING h`2
address in printList(): 0x132606800
DATA: 9421 | STRING 0h`2
address in printList(): 0x132606820
DATA: 992 | STRING Ph`2
address in printList(): 0x132606840
DATA: 301 | STRING
address in printList(): 0x132606860
这是linkedlisttext.txt
4, Hello world
28, What is up world???
2124, your mother
85, more data
9421, just a little more data
992, are we almost there?
301, we did it!
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int data;
char* line;
struct node* next;
} NODE;
struct node* readFile(FILE* fp, NODE* root) {
char line[100];
char* token;
NODE* curr;
NODE* prev;
while(fgets(line, 100, fp) != NULL) {
curr = malloc(sizeof(struct node*));
token = strtok(line, ",");
curr->data = atoi(token);
printf("\ndata in readFile(): %d\n", curr->data);
token = strtok(NULL, "");
curr->line = (char*) malloc(sizeof(token) + 1);
strcpy(curr->line, token);
printf("string in readFile(): %s", curr->line);
printf("address in readFile(): %p\n\n", curr->line);
curr->next = NULL;
if(root == NULL) {
root = curr;
} else {
NODE* travel = root;
while(travel->next != NULL) {
travel = travel->next;
}
travel->next = curr;
}
}
return root;
}
void printList(NODE* root) {
struct node* temp = root;
while(temp->next != NULL) {
printf("DATA: %4d | STRING %s\n", temp->data, temp->line);
printf("address in printList(): %p\n", temp->line);
temp = temp->next;
}
printf("DATA: %4d | STRING %s\n", temp->data, temp->line);
printf("address in printList(): %p\n", temp->line);
}
int main(int argc, char** argv) {
FILE* fp = fopen(argv[1], "r");
NODE* root = NULL;
if(fp == NULL) {
return 0;
}
root = readFile(fp, root);
printList(root);
}
任何帮助表示赞赏,谢谢!
curr->line = (char*) malloc(sizeof(token) + 1);
由于sizeof(token)
始终返回指针的大小(大多数系统为 8 个字节),而不是字符串的长度,因此您没有为line
分配足够的缓冲区。 请尝试:
curr->line = malloc(strlen(token) + 1);
作为旁注,您对主 function 中的行缓冲区和结构成员名称使用相同的名称line
。 它们具有单独的名称空间并且在语法上有效,但可能会为将来的维护造成混淆。
这是高度怀疑的:
curr = malloc(sizeof(struct node*));
您正在为struct node
指针而不是为struct node
分配 memory 。 鉴于 memory 大小的指针和整数,随后分配给curr->data
可能实际上有效,但随后分配给curr->line
和curr->next
可能会给您带来麻烦。
或不。 未定义的行为本质上是不可预测的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.