繁体   English   中英

使用Realloc()和指针时出错

[英]Error Using Realloc() and pointers

我一直在编写程序,但运行时遇到一些问题。 要读取的文件包含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都是字符点,Height和Weight是浮点数(以int为单位)

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

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的代码部分会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM