我正在用C语言编写一个链接列表。这是我的代码。我在这里看不到任何逻辑错误,但是在while循环中打印时,在打印最后一个节点后,它不会跳出循环,而是继续循环,然后给我这个错误“抛出异常:读取访问冲突。 head < / strong>是“ 0xCCCCCCCC。发生在循环中”头”指针上。
// ass0.c
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC // need this to get the line identification
//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); // in main, after local declarations
//NB must be in debug build
#include <crtdbg.h>
#include <stdio.h>
#include <string.h>
typedef enum { FALSE = 0, TRUE } BOOL;
struct Frame {
char* frameName;
struct Frame* pNext;
};
typedef struct {
char* animationName;
struct Frame* frames;
}Animation;
// Forward declarations
void InitAnimation(Animation*);
void InsertFrame(Animation*);
//void DeleteFrame(Animation*);
//void EditFrame(Animation*);
void ReportAnimation(Animation*);
//void CleanUp(Animation*);
int main(void)
{
char response;
BOOL RUNNING = TRUE;
Animation RG;
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
InitAnimation(&RG);
while (RUNNING)
{
printf("MENU\n 1. Insert a Frame at the front\n 2. Delete last Frame\n 3. Edit a Frame\n 4. Report the Animation\n 5. Quit\n");
scanf("%c", &response);
switch (response)
{
case '1':InsertFrame(&RG); break;
//case '2':DeleteFrame(&RG); break;
//case '3':EditFrame(&RG); break;
case '4':ReportAnimation(&RG); break;
//case '5':RUNNING = FALSE; CleanUp(&RG); break;
default:printf("Please enter a valid option\n");
}
}
return 0;
}
void InitAnimation(Animation* newAnimation) {
newAnimation = malloc(sizeof(Animation));
newAnimation->animationName = "Animation_1";
newAnimation->frames = NULL;
}
void InsertFrame(Animation* animation) {
printf("Insert a Frame in the Animation\n");
char fName[50];
printf("Please enter the Frame Name: ");
scanf("%s", fName);
struct Frame *newFrame;
newFrame = malloc(sizeof(struct Frame));
int nameSize = strlen(fName);
newFrame->frameName = malloc(nameSize + 1);
strcpy(newFrame->frameName, fName);
newFrame->pNext = 0;
//struct Frame* head;
if (animation->frames == NULL)
animation->frames = newFrame;
//head = animation->frames;
//head->pNext = NULL;
//head = animation->frames;
else {
//head = animation->frames;
newFrame->pNext = animation->frames;
animation->frames = newFrame;
/*while (head != NULL) {
newFrame->pNext = *head;
animation->frames = newFrame;
printf("%s", *head->frameName);
head = head->pNext;
}*/
//head = animation->frames;
printf("\n");
}
}
void ReportAnimation(Animation* animation) {
printf("Animation name is Animation_1\n");
printf("Report the Animation\n");
struct Frame* head = animation->frames;
//printf("%s\n", head->frameName);
while (head) {
printf("%s\n", head->frameName);
head = head->pNext;
}
//printf("%d", count);
}
答案 0 :(得分:0)
至少函数InitAnimation
没有意义
void InitAnimation(Animation* newAnimation) {
newAnimation = malloc(sizeof(Animation));
newAnimation->animationName = "Animation_1";
newAnimation->frames = NULL;
}
因此整个程序都不正确。
原始对象
Animation RG;
//...
InitAnimation(&RG);
未初始化。该函数的参数只是被覆盖。
由于定义,已经在main中创建了Animation
类型的对象
Animation RG;
您只需要初始化其数据成员。
通过malloc调用删除语句
void InitAnimation(Animation* newAnimation) {
newAnimation->animationName = "Animation_1";
newAnimation->frames = NULL;
}
通常,您应该为数据成员animationName
所指向的字符串动态分配内存。
功能凸轮的外观如下
int InitAnimation( Animation *newAnimation, const char *name )
{
newAnimation->animationName = malloc( strlen( name ) + 1 );
int success = newAnimation->animationName !+ NULL;
if ( success )
{
strcpy( newAnimation->animationName, name );
newAnimation->frames = NULL;
}
return success;
}
并称呼为
Animation RG;
//...
InitAnimation(&RG, "Animation_1" );
请注意,您需要包含标头<stdlib.h>
,在标头中声明函数malloc
和free
。