我目前正在第一次学习C,而且我需要创建一个链接列表。我在过去没有遇到过简单的问题,但功能性抽象和指针传递似乎让我感到困惑,因为我无法理解这一点。运行程序时,我一直收到分段错误。基本上我正在从2个单独的文件中读取数据,然后填充列表。从那里我做了另外的数据,但在这个阶段它是无关紧要的,因为我甚至无法从文件加载数据。使用从主函数传递给各种函数的tm结构来控制列表。
我一直在使用Valgrind和gdb来尝试和诊断,但是对于调试来说,新手很难。经过两天的反复试验,我觉得我有一些重要的事情。这是我的代码和错误消息:
/* global type definitions */
typedef struct tm * tm_type_ptr;
typedef struct stock_list * stock_list_ptr;
typedef struct coin * coin_list_ptr;
typedef struct tm {
coin_list_ptr coins;
stock_list_ptr stock;
} tm_type;
/*coin structs*/
enum coin_types {
FIVE_CENTS=5,
TEN_CENTS=10,
TWENTY_CENTS=20,
FIFTY_CENTS=50,
ONE_DOLLAR=100,
TWO_DOLLARS=200
};
struct coin {
enum coin_types denomination;
unsigned count;
};
/* stock structs*/
struct stock_data
{
char ticket_name[TICKET_NAME_LEN+1];
char ticket_type;
char ticket_zone[TICKET_ZONE_LEN+1];
unsigned int ticket_price;
unsigned int stock_level;
};
typedef struct stock_node
{
struct stock_data * data;
struct stock_node * next_node;
} stock_node;
struct stock_list
{
stock_node * head_stock;
unsigned int num_stock_items;
};
然后填充结构我使用以下函数:
BOOLEAN system_init(tm_type * tm)
{
tm->coins=malloc(sizeof(struct coin) * NUM_COINS);
tm->stock=malloc(sizeof(struct stock_list));
tm->stock->head_stock = malloc(sizeof(stock_node));
tm->stock->head_stock->data = malloc(sizeof (struct stock_data));
tm->coins->count = 0;
tm->stock->num_stock_items =0;
if (tm == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
BOOLEAN load_data(tm_type * tm, char * stockfile, char * coinsfile)
{
FILE *stock_fp = fopen(stockfile, "r");
FILE *coins_fp = fopen(coinsfile, "r");
char *input= NULL, *token = NULL;
unsigned coin_type, amount;
/*stock variables*/
unsigned stock_count=0;
unsigned int ticket_price, stock_level;
char *ticket_name = NULL, ticket_type, *ticket_zone= NULL;
while(fgets(input, buffer_size, coins_fp) != NULL)
{
token= strtok(input, ",");
coin_type = strtol(token, NULL, 10);
if(token != NULL)
{
token= strtok(NULL, ",");
amount = strtol(token, NULL, 10);
}
if(!populate_coin_array(tm, coin_type, amount))
{
printf("OMG YOU SUCK!\n");
}
}
}
/*display_coin_array(tm); */
}
fclose(coins_fp);
free(input);
/*read stock file and populate stock structure */
if(fseek(stock_fp ,0, SEEK_END) == 0)
{
long buffer_size = ftell(stock_fp);
if(buffer_size == -1)
{
printf("Error! Exiting!\n");
return FALSE;
}
input = malloc(sizeof(char) * (buffer_size+1)); /*allocate buffer size plus one for ` '\0'*/
if(fseek(stock_fp,0,SEEK_SET) == 0)
{
while(fgets(input, buffer_size, stock_fp) != NULL)
{
ticket_name = strtok(input, ",");
ticket_type = *(strtok(NULL, ","));
ticket_zone = strtok(NULL, ",");
token = strtok(NULL, ",");
ticket_price = strtol(token, NULL, 10);
token = strtok(NULL,",");
stock_level = strtol(token, NULL, 10);
stock_count++;
if(!populate_stock_list(tm, ticket_name, ticket_type, ticket_zone,
ticket_price, stock_level, stock_count))
{
printf("ERROR!!!\n");
}
}
}
display_stock_list(tm, stock_count);
}
fclose(stock_fp);
free(input);
return TRUE;
}
BOOLEAN populate_coin_array(tm_type *tm, unsigned coin_type, unsigned amount)
{
struct coin single_coin;
struct coin *coin_array;
coin_array = malloc(sizeof(struct coin) * NUM_COINS);
single_coin = *(tm->coins);
single_coin.denomination = coin_type;
single_coin.count = amount;
*coin_array = single_coin;
tm->coins = coin_array;
return TRUE;
}
BOOLEAN populate_stock_list(tm_type *tm, char *ticket_name, char ticket_type,
char *ticket_zone, unsigned int ticket_price, unsigned int stock_level,
unsigned int stock_count)
{
tm_type temp_list;
struct stock_node *new_node;
temp_list = *tm;
new_node = malloc(sizeof(stock_node*));
if(new_node != NULL)
{
strcpy(new_node->data->ticket_name, ticket_name);
new_node->data->ticket_type = ticket_type;
strcpy(new_node->data->ticket_zone , ticket_zone);
new_node->data->ticket_price = ticket_price;
new_node->data->stock_level = stock_level;
new_node->next_node = temp_list.stock->head_stock;
temp_list.stock->head_stock = new_node;
temp_list.stock->num_stock_items = stock_count;
}
return TRUE;
}
我很抱歉大量的乱码,我从一些.c和.h文件中取出它。我试图尽可能地削减它以便更容易进行故障排除。
Valgrind正在给我这个:
==16716== Use of uninitialised value of size 8
==16716== at 0x4C2BFFC: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
==16716== by 0x400D37: populate_stock_list (tm_stock.c:37)
==16716== by 0x40128F: load_data (tm_utility.c:129)
==16716== by 0x40098F: main (tm.c:57)
==16716==
==16716== Invalid write of size 1
==16716== at 0x4C2BFFC: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
==16716== by 0x400D37: populate_stock_list (tm_stock.c:37)
==16716== by 0x40128F: load_data (tm_utility.c:129)
==16716== by 0x40098F: main (tm.c:57)
==16716== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==16716==
==16716==
==16716== Process terminating with default action of signal 11 (SIGSEGV)
==16716== Access not within mapped region at address 0x0
==16716== at 0x4C2BFFC: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
==16716== by 0x400D37: populate_stock_list (tm_stock.c:37)
==16716== by 0x40128F: load_data (tm_utility.c:129)
==16716== by 0x40098F: main (tm.c:57)
可以帮我指出正确的方向吗?我不希望有人给我答案,我只想知道为什么会这样。
谢谢!
答案 0 :(得分:1)
在使用之前,您需要为new_node->data
分配struct stock_data *
。
此外,在malloc()
传递结构大小中,您分配的不是其指针的大小。
更新将是
//--------------------------------v no * here
new_node = malloc(sizeof(*new_node));
if(new_node != NULL)
{
new_node->data = malloc(sizeof(*(new_node->data)));
...
//your code