gpt4 book ai didi

打印元素后崩溃

转载 作者:行者123 更新时间:2023-11-30 20:00:03 24 4
gpt4 key购买 nike

我的程序在运行时崩溃并出现以下错误:

"Exception thrown at 0x777433D5 (ntdll.dll) in caza.exe: 0xC0000005: Access violation reading location 0x00000003."

出了什么问题?

我尝试过在 main 中使用该免费函数,也尝试过不使用它,但都遇到了相同的错误。

#include <stdio.h>
#include <malloc.h>

typedef struct Dreptunghi
{
int x1, y1, x2, y2;
}Dreptunghi;

void readElements(Dreptunghi* pDreptunghi, int nDreptunghi)
{


for (int i = 0; i < nDreptunghi; ++i)
{
printf("Introduceti valorile pentru dreptunghiul %d \n", i + 1);
scanf("%d %d %d %d",
&pDreptunghi[i].x1,
&pDreptunghi[i].y1,
&pDreptunghi[i].x2,
&pDreptunghi[i].y2);
}

printf("\n");
}

void printElements(Dreptunghi* pDreptunghi, int nDreptunghi)
{
for (int i = 0; i < nDreptunghi; ++i)
{
printf("%d %d %d %d\n",
pDreptunghi[i].x1,
pDreptunghi[i].y1,
pDreptunghi[i].x2,
pDreptunghi[i].y2);
}
}

int main()
{
Dreptunghi* pDreptunghi = NULL;
int n=0;
scanf("%d", &n);
pDreptunghi = (Dreptunghi*)malloc(n+1);
readElements(pDreptunghi, n);
printElements(pDreptunghi, n);
free(pDreptunghi);
return 0;
}

最佳答案

改变

pDreptunghi = (Dreptunghi*)malloc(n+1);

pDreptunghi = malloc( (sizeof(*pDreptunghi)*n) + 1);

关于打印元素后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com