title: C - Realloc Fails because "enot enough space"
body: <p>iam actually failing on a realloc. Perror returns a "not enough space" message, which i cant really explain myself.</p>
  
  <p>I compile with Mingw32 on W10. No Optimizions(with -wall, wextra).</p>
  
  <p>Its for simple snake game. Iam step for step implenting some functions. Actually i wanne simple grow up snake. ( Snake got a Head Block, and some Body Blocks).</p>
  
  <p>At Init, i allocate 5 Snake Blocks with malloc.
  SNAKE_START_BLOCKS is 5 per #define</p>
  
  <p>My definition for a Block:
  </p>
  
  <pre><code>// Structs
  typedef struct {
      float x;
      float y;
      float w;
      float h;
  } Block;
  </code></pre>
  
  <p>Here:
  </p>
  
  <pre><code>GameSnake.SB_Body = malloc(SNAKE_START_BLOCKS * sizeof(Block));
  
      if(GameSnake.SB_Body == NULL)
      {
          printf("Game Subsystem Fehler 'GameSnake': Kann Speicher fuer Blocks nicht laden!\n");
          return false;
      }
  
      size_t i;
      for(i = 0; i&lt;SNAKE_START_BLOCKS; i++)
      {
          GameSnake.SB_Body[i].w = SNAKE_PICTURE_SIZE;
          GameSnake.SB_Body[i].h = SNAKE_PICTURE_SIZE;
          GameSnake.SB_Body[i].x = GameSnake.SB_Head.x - (((float)i*(float)SNAKE_PICTURE_SIZE/Level.TL_X_Size)+(float)SNAKE_PICTURE_SIZE/Level.TL_X_Size);
          GameSnake.SB_Body[i].y = GameSnake.SB_Head.y;
      }
  
      GameSnake.SB_Body_Elements = SNAKE_START_BLOCKS;
  </code></pre>
  
  <p>Some Codeline later, if i got a hit to my food. I Call my Grow Function</p>
  
  <pre class="lang-c prettyprint-override"><code>// Lets grow the Snake..
  bool CSFMLGrowSnake()
  {
      GameSnake.SB_Body = realloc(GameSnake.SB_Body, (GameSnake.SB_Body_Elements+1) * sizeof(Block));
  
      if(GameSnake.SB_Body == NULL)
      {
          printf("Game Subsystem Fehler 'GameSnake': Kann keinen zusaetzlichen Speicher fuer die Snake anfordern!\n");
          perror("Genauer Fehler: ");
          return false;
      }
      else
      {
          GameSnake.SB_Body[GameSnake.SB_Body_Elements].w = SNAKE_PICTURE_SIZE;
          GameSnake.SB_Body[GameSnake.SB_Body_Elements].h = SNAKE_PICTURE_SIZE;
  
          // Check Direction and Setup the new Block
          // Todo: Prevent New Block appears in tile Border/in Snake/in Item
          if(GameSnake.S_Actual_Direction == UP)
          {
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].x = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].x;
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].y = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].y + (float)SNAKE_PICTURE_SIZE/Level.TL_Y_Size;
          }
          else if(GameSnake.S_Actual_Direction == DOWN)
          {
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].x = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].x;
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].y = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].y - (float)SNAKE_PICTURE_SIZE/Level.TL_Y_Size;
          }
          else if(GameSnake.S_Actual_Direction == RIGHT)
          {
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].x = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].x - (float)SNAKE_PICTURE_SIZE/Level.TL_X_Size;
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].y = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].y;
          }
          else if(GameSnake.S_Actual_Direction == LEFT)
          {
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].x = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].x + (float)SNAKE_PICTURE_SIZE/Level.TL_X_Size;
              GameSnake.SB_Body[GameSnake.SB_Body_Elements].y = GameSnake.SB_Body[GameSnake.SB_Body_Elements-1].y;
          }
  
      }
  
      GameSnake.SB_Body_Elements++;
  
      return true;
  }
  </code></pre>
  
  <p>I checked up my array indices. But that seems ok.
  Then i checked Element Adresses at Initialization:</p>
  
  <pre><code>Pointer Adress[0]: 40900000
  Pointer Adress[1]: 40800000
  Pointer Adress[2]: 40600000
  Pointer Adress[3]: 40400000
  Pointer Adress[4]: 40200000
  </code></pre>
  
  <p>Which confuse me a bit, is that "decreasing" of Adress normal?! Should it be reversal?</p>
  
  <p>Sometime it works, but if it works, after a few seconds, it leads to segmentation fault. If i print addresses in my grow functions, the newest element will have an adress of 0.</p>
  
  <p>But in Most cases, it directly fails on the realloc, with the
  perror message "not enough space".</p>
  
  <p>Iam actually unsure where the problem is. Maybe you can help me or give me a tip. I cant believe that additional 16 Bytes will overflow my memory :O</p>
  
  <p>I have a repo at github, if you want to view the whole file here is it:
  <a href="https://github.com/MasterOfPenetrator/SnakeGame/blob/master/CSFMLGameSnake.c" rel="nofollow noreferrer">https://github.com/MasterOfPenetrator/SnakeGame/blob/master/CSFMLGameSnake.c</a></p>
  
  <p>Every help is welcome !</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Some Details from the Debugger</strong></p>
  
  <pre><code>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 &lt;realloc&gt;
  
  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;
  </code></pre>
  
  <p>And now iam confused...</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I found the error, shame on me :(,
  In my PopBlocks Function i did the horrible fail:</p>
  
  <pre><code>for(i = GameSnake.SB_Body_Elements; i&gt;0; i--)
              /\
              ||
  I should append a "-1", now it works fine ;)
  </code></pre>
  
  <p>Iam wondering how long a illegal access work ;-)
  This Function is called by every move ;)</p>
  
  <p>thanks for help!!</p>