Stardust741953 2018-04-05 06:45 采纳率: 50%
浏览 6695
已采纳

引发的异常: 0xC0000005: 写入位置 0xCDCDCDCD 时发生访问冲突。

#include
#include
#include

#define LEN sizeof(struct student)

struct student *creat();
void print(struct student *head);

struct student
{
int num;
float score;
struct student *next;
};

int n;

void main()
{
struct student *stu;

stu = creat();
print( stu );

printf("\n\n");
system("pause");

}

struct student *creat()
{
struct student *head;
struct student *p1, *p2;

p1 = p2 = (struct student *)malloc(LEN);

printf("please enter the num:\n");
scanf_s("%d", p1->num,sizeof(p1->num));
printf("please enter the score:\n");
scanf_s("%f", p1->score,sizeof(p1->score));

head = NULL;
n = 0;

while (p1->num)
{
    n++;
    if (n == 1)
    {
        head = p1;
    }
    else
    {
        p2->next = p1;
    }
    p2 = p1;
    p1 = (struct student *)malloc(LEN);

    printf("please enter the num:\n");
    scanf_s("%d", p1->num, sizeof(p1->num));
    printf("please enter the score:\n");
    scanf_s("%f", p1->score, sizeof(p1->score));
}
p2->next = NULL;

return head;

}

void print(struct student *head)
{
struct student *p;
p = head;
while (p->next)
{
printf("学号: %d 分数: %f\n", p->num, p->score);
p = p->next;
}
}

运行环境为vs2017,win10 64位。在运行到第一个scanf_s处是报的错:
0x0F86742E (ucrtbased.dll)处(位于 lianbiao.exe 中)引发的异常: 0xC0000005: 写入位置 0xCDCDCDCD 时发生访问冲突。

看见有人说是引用了未初始化的内存,那么怎么初始化呢?试过calloc函数,报错如下:
图片说明

求大佬教教,感激不尽!

  • 写回答

2条回答

  • SecretGarden 2018-04-05 08:43
    关注

    所有的scanf函数后面的变量都应该取地址,例如:
    将:

    scanf_s("%d", p1->num, sizeof(p1->num));
    

    改为:

    scanf_s("%d", &p1->num, sizeof(p1->num));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP