C-Realloc失败,因为;“没有足够的空间”;
我真的在一个真实的地方失败了。Perror返回一条“空间不足”的消息,我无法真正解释我自己 我在W10上使用Mingw32进行编译。没有优化(使用-wall,wextra) 这是一个简单的蛇游戏。Iam一步一步地实现一些功能。事实上我想要一条简单的成年蛇。(蛇有一个头部挡块和一些身体挡块) 在Init中,我用malloc分配了5个Snake块。 SNAKE_START_block为5个/define 我对块的定义: 在这里: 我检查了我的数组索引。但这似乎没问题。 然后我在初始化时检查了元素地址:C-Realloc失败,因为;“没有足够的空间”;,c,realloc,C,Realloc,我真的在一个真实的地方失败了。Perror返回一条“空间不足”的消息,我无法真正解释我自己 我在W10上使用Mingw32进行编译。没有优化(使用-wall,wextra) 这是一个简单的蛇游戏。Iam一步一步地实现一些功能。事实上我想要一条简单的成年蛇。(蛇有一个头部挡块和一些身体挡块) 在Init中,我用malloc分配了5个Snake块。 SNAKE_START_block为5个/define 我对块的定义: 在这里: 我检查了我的数组索引。但这似乎没问题。 然后我在初始化时检查了元素
Pointer Adress[0]: 40900000
Pointer Adress[1]: 40800000
Pointer Adress[2]: 40600000
Pointer Adress[3]: 40400000
Pointer Adress[4]: 40200000
这让我有点困惑,地址的“减少”正常吗?!应该是反转吗
有时它是有效的,但如果它有效,几秒钟后,就会导致分段错误。如果在grow函数中打印地址,则最新元素的地址为0
但在大多数情况下,它直接在realloc上失败,并且
或消息“空间不足”
实际上我不确定问题出在哪里。也许你可以帮我或者给我小费。我不敢相信额外的16个字节会使我的内存溢出:O
我在github有一份回购协议,如果您想查看整个文件,请点击这里:
欢迎大家帮忙
提前谢谢
调试器中的一些详细信息
Disassembly:
At this line it breaks:
0x4045b4 mov 0x41026c,%eax
0x4045b9 add $0x1,%eax
0x4045bc shl $0x4,%eax
0x4045bf mov %eax,%edx
0x4045c1 mov 0x4101d0,%eax
0x4045c6 mov %edx,0x4(%esp)
0x4045ca mov %eax,(%esp)
0x4045cd call 0x40b900 <realloc>
Debugger says:
Failure finding "Stack level "
Failure matching reg_output
This is the content of the adress
0x41026c: 05 00 00 00 59 6f 75 72|65 20 43 6f 6f 6c 21 00 ....Youre Cool!.
0x41027c: 00 00 00 00 00 00 00 00|64 00 00 00 0e 01 00 00 ........d.......
which is setted in my init here
strcpy(GameSnake.S_Name, "Youre Cool!");
The structure is here:
typedef struct {
Block SB_Head;
Block *SB_Body;
sfTexture *SB_Head_Texture;
sfShader *SB_Head_Shader;
sfRenderStates SB_Head_State;
sfTexture *SB_Body_Texture;
sfShader *SB_Body_Shader;
sfRenderStates SB_Body_State;
size_t SB_Body_Elements;
char S_Name[50];
int S_Health;
int S_Rotate;
int S_Score;
float S_Speed;
float S_DefaultSpeed;
float S_LightDistance;
bool S_Light_FBM;
bool S_Light_OnOff;
Direction S_Actual_Direction;
Direction S_Prev_Direction;
bool S_Is_Init;
bool S_Is_Dead;
bool S_GODMODE;
bool S_NOCLIP;
float S_EndItemTime;
} Snake;
我想知道非法访问的有效期有多长;-)
每次移动都会调用此函数;)
谢谢你的帮助 编辑:大家好,在我的问题中,问候被忽略了:您必须在失败后直接检查
errno
(这就是perror
获取错误的原因)。调用printf
后,errno
的值不确定。请在调试器中运行。当它破坏打印GameSnake.SB_Body_Elements
时,我不知道MinGW的使用如何影响这一点,但在Windows 7或10上运行32位MSVC编译时,程序可用的总内存(静态+动态)少于2Gb。ERRNO说ENOMEM,我从来没有达到2Gb,在RAM上我得到了大约20MB,VRAM是~50MB(iGPU).编辑:大家好,在我的问题中,问候被忽略了:您必须在失败后直接检查errno
(这就是perror
获取错误的原因)。调用printf
后,errno
的值不确定。请在调试器中运行。当它破坏printGameSnake.SB_Body_Elements
时,我不知道MinGW的使用如何影响这一点,但在Windows7或10上运行32位MSVC编译时,程序可用的总内存(静态+动态)少于2Gb。ERRNO说ENOMEM,我从来没有达到2Gb,在RAM上我得到了大约20MB,VRAM是~50MB(iGPU)。
Pointer Adress[0]: 40900000
Pointer Adress[1]: 40800000
Pointer Adress[2]: 40600000
Pointer Adress[3]: 40400000
Pointer Adress[4]: 40200000
Disassembly:
At this line it breaks:
0x4045b4 mov 0x41026c,%eax
0x4045b9 add $0x1,%eax
0x4045bc shl $0x4,%eax
0x4045bf mov %eax,%edx
0x4045c1 mov 0x4101d0,%eax
0x4045c6 mov %edx,0x4(%esp)
0x4045ca mov %eax,(%esp)
0x4045cd call 0x40b900 <realloc>
Debugger says:
Failure finding "Stack level "
Failure matching reg_output
This is the content of the adress
0x41026c: 05 00 00 00 59 6f 75 72|65 20 43 6f 6f 6c 21 00 ....Youre Cool!.
0x41027c: 00 00 00 00 00 00 00 00|64 00 00 00 0e 01 00 00 ........d.......
which is setted in my init here
strcpy(GameSnake.S_Name, "Youre Cool!");
The structure is here:
typedef struct {
Block SB_Head;
Block *SB_Body;
sfTexture *SB_Head_Texture;
sfShader *SB_Head_Shader;
sfRenderStates SB_Head_State;
sfTexture *SB_Body_Texture;
sfShader *SB_Body_Shader;
sfRenderStates SB_Body_State;
size_t SB_Body_Elements;
char S_Name[50];
int S_Health;
int S_Rotate;
int S_Score;
float S_Speed;
float S_DefaultSpeed;
float S_LightDistance;
bool S_Light_FBM;
bool S_Light_OnOff;
Direction S_Actual_Direction;
Direction S_Prev_Direction;
bool S_Is_Init;
bool S_Is_Dead;
bool S_GODMODE;
bool S_NOCLIP;
float S_EndItemTime;
} Snake;
for(i = GameSnake.SB_Body_Elements; i>0; i--)
/\
||
I should append a "-1", now it works fine ;)