StackOverflow 上的第一篇文章(行业测试员,业余时间非常糟糕的程序员)。出于披露目的,这是我正在努力解决的大学作业(据我所知,在这里提问并不被禁止。
无论如何,我有一个程序从文本文件中读取一行,对行数据进行标记,创建一个链表,然后将每个标记(2 个字符串,1 个浮点数,1 个无符号)插入节点。一切都很好,直到节点使用的内存被释放 - 整个程序崩溃。调试后,我似乎将问题与两个字符串复制操作隔离开来。它们看起来都非常有效,但是 free() 根本不喜欢它们。试过 strncpy() - 没有区别。尝试按字符复制字符串 char - 没有区别。现在我很茫然...
下面的代码,如果有人想看看(哦,更多披露 - 几乎是一个完整的 C n00b,所以是的,如果你看到下面的不良做法,那就是我......)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "ts.h"
int main(void)
{
/* Variables */
FILE *stream;
char buf[BUFSIZ];
/* Open stock file */ //TODO - make this dynamic, don't hard-code file name
stream = fopen("stock.csv", "r");
assert(stream);
/* Using addStockNode */
while (fgets(buf, BUFSIZ, stream))
{
addStockNode(buf);
}
}
void addStockNode(char* stockLine)
{
//Create linked list
StockNodePtr head, new, current, previous, nextStock;
unsigned listSize;
char *stkTok1, *stkTok2, *stkTok3, *stkTok4;
unsigned stkLevel;
int i;
float stkPrice;
listSize = 0;
head = NULL;
/* Create new stock node */
if ((new = malloc(sizeof(StockNodePtr))) == NULL)
{
fprintf(stderr,"\nMemory allocation for node insertion failed\n");
fprintf(stderr,"Aborting\n");
exit(EXIT_FAILURE);
}
/* Tokenise data */
stkTok1 = strtok(stockLine, ",");
stkTok2 = strtok(NULL, ",");
stkTok3 = strtok(NULL, ",");
stkTok4 = strtok(NULL, ",");
/* Search to find where in insert new list node */ //TODO - needs to be adapted to sort by stock DESCRIPTION
current = head;
previous = NULL;
/* stockID */
// strcpy(new->stockID, stkTok1); //falls over at free()
// strncpy(new->stockID, stkTok1, STOCKID_LEN); //falls over at free()
for(i = 0; i < strlen(stkTok1); i++) //still falls over at free()
{
new->stockID[i] = stkTok1[i];
}
/* description */
// strcpy(new->description, stkTok2); //falls over at free()
/* unitPrice */
stkPrice = strtof(stkTok3, NULL);
new->unitPrice = stkPrice;
/* StockLevel */
stkLevel = strtol(stkTok4, NULL, 10);
new->stockLevel = stkLevel;
/*nextStock */
new->nextStock = current;
/* Increment listSize */
listSize++;
//TAKE OUT LATER - loadData can iterate through each line of the file */
if(previous == NULL)
{
head = new;
}
else
{
previous->nextStock = new;
}
/* Print node details */
current = head;
printf("%s,%s,%f,%i\n", current->stockID, current->description, current->unitPrice, current->stockLevel);
/* Deallocate memory used by node */
current = head;
while(current != NULL)
{
nextStock = current->nextStock;
free(current); //EXECUTE THIS, IT FALLS OVER (with strcpy lines uncommented)
current = nextStock;
}
return EXIT_SUCCESS;
}*
为了完整起见,这里是股票节点结构......
typedef struct stockNode
{
char stockID[STOCKID_LEN + 1];
char description[DESCRIPTION_MAX + 1];
float unitPrice;
unsigned stockLevel;
StockNodePtr nextStock;
} StockNodeType;
如果有人能指出我哪里出错了,我将不胜感激!
编辑 - 这是股票节点常量......
#define STOCKID_LEN 5
#define DESCRIPTION_MAX 40
#define PRICE_COLWIDTH 7
#define STOCKLEVEL_COLWIDTH 3
#define STOCKLEVEL_MAX 100
哦,还有正在添加的股票数据(并不是说它有什么问题)......
S0001,Slazenger Classic Racquet,150.00,5
S0002,Slazenger Lite Racquet,98.00,3
S0003,Wilson Tournament Gold Balls,14.95,20
S0004,Dunlop Grand Prix Balls,10.95,25
S0005,Luft Nemesis Racquet,125.00,1
S0006,Wilson Tournament Balls,12.95,12