需要帮助traking down运行时检查失败#2 - 围绕变量'列表'堆栈已损坏
问题描述:
所以我得到运行时检查失败#2 - 围绕变量'列表'堆栈已损坏。 我已经通过代码挖,并找不出这个错误发生的地方。有人可以看看这个,告诉我我哪里可能会出问题。首先,这是一个数据结构和算法类的类分配。老师已经接受了我的代码,但告诉我我需要修复这个错误。我一直在寻找几天,似乎无法找到它在哪里。这个项目的唯一限制是主体不能改变。 感谢您的帮助。需要帮助traking down运行时检查失败#2 - 围绕变量'列表'堆栈已损坏
杰森
list.h
typedef char Titem;
// Interface of list
typedef struct node *Tpointer;
typedef struct node {
Titem item;
Tpointer next;
Tpointer first;
} Tnode;
typedef Tpointer Tlist;
void initialize_list (Tlist *list);
void insert_to_list_end(Tlist *list, Titem data);
void print_list (Tlist *list);
void cleanup_list(Tlist *list);
list.c
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
// Implementation of list (only obj is need in appl)
void initialize_list (Tlist list)
{
list->first = NULL;
list->next = NULL;
}
void cleanup_list(Tlist list)
{
Tpointer aux1, aux2;
aux1 = list->first;
while (aux1 != NULL)
{
aux2 = aux1->next;
free(aux1);
printf("\nDeleted"); //for testing purposes
aux1 = aux2;
}
initialize_list(&list);
}
void insert_to_list_end(Tlist list, Titem data)
{
Tpointer newnode;
newnode = (Tpointer) malloc(sizeof(Tnode));
newnode -> item = data;
if (list->first == NULL)
list->first = newnode; //first node
else
list->next->next = newnode; //not first node
list->next = newnode;
list->next->next = NULL;
}
void print_list (Tlist list)
{
Tpointer what;
printf("\nList 4 :");
what = list->first;
while (what != NULL)
{
printf("%c ", what->item);
printf("\nNext is %d ", what->next);
what = what->next;
}
}
的main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
// Application
int main (void) {
Tlist list;
initialize_list(&list);
insert_to_list_end(&list, 'a');
insert_to_list_end(&list, 'b');
insert_to_list_end(&list, 'c');
insert_to_list_end(&list, 'd');
print_list(&list);
cleanup_list(&list);
fflush(stdin); getchar();
return 0;
}
答
一个成熟的编译器甚至不编译list.c -
list.c:6: error: conflicting types for 'initialize_list' list.h:13: note: previous declaration of 'initialize_list' was here list.c:12: error: conflicting types for 'cleanup_list' list.h:16: note: previous declaration of 'cleanup_list' was here list.c: In function 'cleanup_list': list.c:24: warning: passing argument 1 of 'initialize_list' from incompatible pointer type list.c:6: note: expected 'Tlist' but argument is of type 'struct node **' list.c: At top level: list.c:27: error: conflicting types for 'insert_to_list_end' list.h:14: note: previous declaration of 'insert_to_list_end' was here list.c:41: error: conflicting types for 'print_list' list.h:15: note: previous declaration of 'print_list' was here
- 这里是一个修正版本:
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
void initialize_list(Tlist *list)
{
*list = NULL;
}
void cleanup_list(Tlist *list)
{
Tpointer aux1, aux2;
if (!*list) return;
aux1 = (*list)->first;
while (aux1 != NULL)
{
aux2 = aux1->next;
free(aux1);
printf("\nDeleted"); //for testing purposes
aux1 = aux2;
}
initialize_list(list);
}
void insert_to_list_end(Tlist *list, Titem data)
{
Tpointer newnode;
newnode = malloc(sizeof(Tnode));
newnode->item = data;
if (*list == NULL) //first node
newnode->first = newnode;
else //not first node
((*list)->next = newnode)->first = (*list)->first;
newnode->next = NULL;
*list = newnode;
}
void print_list(Tlist *list)
{
Tpointer what;
printf("\nList 4 :");
if (!*list) return;
what = (*list)->first;
while (what != NULL)
{
printf("%c ", what->item);
printf("\nNext is %d ", what->next);
what = what->next;
}
}
'fflush(标准输入);'导致未定义Behavior.I想知道你的老师并没有指出。 – 2012-03-28 09:12:07
老师写了main.c. – Xintaris 2012-03-28 09:15:51
'list.h'中的函数声明不合适:'Tlist * list'应该被'Tlist list'替换。你能提供关于你的错误的更多细节吗? – 2012-03-28 09:18:20