gpt4 book ai didi

c - 使用 Realloc() 和指针时出错

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:04 27 4
gpt4 key购买 nike

我一直在编写程序,但在运行时遇到了一些问题。要读取的文件包含 11 列,但要读取的行数未指定。

我一直在使用 realloc() 来创建动态内存以更改读取的输入文件。但是,在运行该程序后,我收到以下跟踪信息。我不知道这意味着什么,也不知道如何解决。该程序编译正常,但中止内存转储和此屏幕。如果需要,我可以上传整个代码,但是它很长。

 do //Safe to assume atleast one line of information will be present
{
j++;
if(j>5) //Will resize the array if there are more than 5 rows
{
Poke = realloc(Poke, j * 100); //* 100 needed to account for strings
Type = realloc(Type, j * 100);
Height = realloc(Height,j);
Weight = realloc(Weight,j);
HP = realloc(HP, j);
Attack = realloc(Attack, j);
Defense = realloc(Defense, j);
SPA = realloc(SPA, j);
SPD = realloc(SPD, j);
Speed = realloc(Speed, j);
Evo = realloc(Evo, j * 100);
}

//Read and store all values
fscanf(ifp, "%s %s %f %f %i %i %i %i %i %i %s", (Poke + 100*j),
(Type + 100*j),(Height + j),(Weight + j),(HP + j),(Attack + j),
(Defense + j),(SPA + j),(SPD + j),(Speed + j),(Evo + 100*j));
} while(!feof(ifp));

其中 Poke、Type 和 Evo 都是角色点数高度和体重是 float 的休息一下

来自文件的输入数据是这种形式

Charmander  Fire        0.6     8.5     39  52      43      60      50      65      Charmeleon
Bulbasaur Grass 0.7 6.9 45 49 49 65 65 45 Ivysaur

最佳答案

错误似乎是说您传递了零作为 realloc 的大小(realloc(): invalid next size: 0x00000000)。您的代码是否会错误地尝试将新大小设置为无效的值?在调用 realloc 之前尝试打印出大小并确保它符合您的预期。

如果不是这样,那么至少显示调用 realloc 的代码部分会有所帮助。

关于c - 使用 Realloc() 和指针时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902571/

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