这 2 个错误与链表/低级文件读取/strtok 程序有关。
==10982== Use of uninitialised value of size 4
==10982== at 0x40C3899: strtok (strtok.S:197)
==10982== by 0x8048719: main (main.c:9)
==10982== Uninitialised value was created by a stack allocation
==10982== at 0x80487D2: buildList (functions.c:13)
==10982==
==10982== Use of uninitialised value of size 4
==10982== at 0x40C38C1: strtok (strtok.S:223)
==10982== by 0x8048719: main (main.c:9)
==10982== Uninitialised value was created by a stack allocation
==10982== at 0x80487D2: buildList (functions.c:13)
==10982==
==10982== Conditional jump or move depends on uninitialised value(s)
==10982== at 0x40C38C4: strtok (strtok.S:224)
==10982== by 0x8048719: main (main.c:9)
==10982== Uninitialised value was created by a stack allocation
==10982== at 0x80487D2: buildList (functions.c:13)
这是函数 buildList
void buildList(int fdin, int lines)
{
char ara[4096];
int num;
double p;
read(fdin, ara, 4096);
char *nl = "\n";
char *ws = " ";
char *temp = strtok(ara, " ");
while(temp != NULL)
{
Stock *s = (Stock*)calloc(1, sizeof(Stock));
s->c.symbol = (char*)calloc(strlen(temp)+1, sizeof(char));
strcpy(s->c.symbol, temp);
temp = strtok(NULL, "\n");
s->c.name = (char*)calloc(strlen(temp)+1, sizeof(char));
strcpy(s->c.name, temp);
temp = strtok(NULL, "\n");
sscanf(temp, "%lf", &p);
temp = strtok(NULL, "\n");
s->price = p;
sscanf(temp, "%d", &num);
s->shares = num;
Node *n = (Node*)calloc(1, sizeof(Node));
n->data = s;
addOrdered(n);
temp = strtok(NULL, " ");
}
close(fdin);
}
我无法弄清楚为什么会发生此错误。从我读过的内容来看,这是因为我将事物分配给 strtok 中的 char* 而没有为它们分配任何内存。然而,这就是我过去的做法,我认为这很好。