我正在用C写一个链表。这是我的代码。我在这里看不到任何逻辑错误,但是在while循环中打印时,在打印完最后一个节点后,它不会跳出循环,而是继续循环,然后给我这个错误“抛出异常:读取访问冲突。头为0xCCCCCCCC。在循环的“ head”指针上。

// 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);
  }

最佳答案

至少函数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>,其中声明了函数mallocfree

关于c - 引发异常:读取访问冲突。头是0xCCCCCCCC。发生,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57891697/

10-17 02:43