抛出异常:读取访问冲突。 **head** 是 0xCCCCCCCC。发生了

Exception thrown: read access violation. **head** was 0xCCCCCCCC. occurred

我正在用 C 编写链表。这是我的代码。我在这里真的看不到任何逻辑错误但是在 while 循环中打印时,在打印最后一个节点后它不会跳出循环但继续循环然后在 "head" 指针上给我这个错误 "Exception thrown: read access violation. head was 0xCCCCCCCC. occurred"在循环中。

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

因此整个程序是不正确的。

原object

Animation RG;
//...
InitAnimation(&RG);

未初始化。函数的参数只是被覆盖了。

由于定义

,类型 Animation 的 object 已经在 main 中创建
Animation RG;

你只需要初始化它的数据成员。

删除带有 malloc 调用的语句

void InitAnimation(Animation* newAnimation) {
newAnimation->animationName = "Animation_1";
newAnimation->frames = NULL;
 }

一般来说,您应该为数据成员指向的字符串动态分配内存 animationName

函数 cam 如下所示

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" );

注意你需要包含 header <stdlib.h> 函数 mallocfree 声明的地方。