[英]Printing Linked List leads to strings with garbage values (C)
I was reading a linked list from a.txt file to fill the following node:我正在从 a.txt 文件中读取一个链表来填充以下节点:
typedef struct node {
int data;
char* line;
struct node* next;
} NODE;
The code works fine and reads the file into the linked list.该代码工作正常并将文件读入链接列表。 I print out the string while each node is being initialized.
我在初始化每个节点时打印出字符串。 However whenever I call the
printList()
function, the line
string is always a garbage value.但是,每当我调用
printList()
function 时, line
字符串始终是垃圾值。 Sample run:样品运行:
./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
Here is linkedlisttext.txt
这是
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!
and here is the code:这是代码:
#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);
}
Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!
curr->line = (char*) malloc(sizeof(token) + 1);
curr->line = (char*) malloc(sizeof(token) + 1);
As sizeof(token)
always returns the size of pointer (8 bytes with most systems), not the length of the string, you are not allocating the sufficient buffer for line
.由于
sizeof(token)
始终返回指针的大小(大多数系统为 8 个字节),而不是字符串的长度,因此您没有为line
分配足够的缓冲区。 Please try instead:请尝试:
curr->line = malloc(strlen(token) + 1);
As a side note, you are using the same name line
for both the line buffer in the main function and the structure member name.作为旁注,您对主 function 中的行缓冲区和结构成员名称使用相同的名称
line
。 They have individual namespaces and syntactically valid, but might be confusing for the future maintenance.它们具有单独的名称空间并且在语法上有效,但可能会为将来的维护造成混淆。
This is highly suspect:这是高度怀疑的:
curr = malloc(sizeof(struct node*));
You are allocating memory for a struct node
pointer but not for a struct node
.您正在为
struct node
指针而不是为struct node
分配 memory 。 Given the memory sizes of pointers and ints, subsequently assigning to curr->data
may actually work, but then assigning to curr->line
and curr->next
is likely to get you in trouble.鉴于 memory 大小的指针和整数,随后分配给
curr->data
可能实际上有效,但随后分配给curr->line
和curr->next
可能会给您带来麻烦。
Or not.或不。 Undefined behavior is inherently unpredictable.
未定义的行为本质上是不可预测的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.