title: Random memory acces violation
body: <p>I've this sample code :</p>
  
  <pre><code>map *d;
  
  i = MAP_SIZE;
  j = sizeof(map);
  d = malloc(MAP_SIZE);
  
  if (d == NULL) {
      exit(EXIT_FAILURE);
  }
  
  dest.x = dest.y = 0;
  
  for (i = 0; i &lt; WINDOW_HEIGHT / AREA_RESOLUTION; i++)
  {
      for (j = 0; j &lt; WINDOW_WIDTH / AREA_RESOLUTION; j++)
      {
          k = GetAreaPos(j, i);
          Area = d[k];
          dest.x = j*AREA_RESOLUTION;
          dest.y = i*AREA_RESOLUTION;
          if (Area-&gt;landType == DESTRUCTIBLE_BRICK) {      //GOT ERROR HERE
              SDL_QueryTexture(Game_Texture-&gt;Explodable, NULL, NULL, &amp;dest.w, &amp;dest.h);
              SDL_RenderCopy(Renderer, Game_Texture-&gt;Explodable, NULL, &amp;dest);
          }
          if (Area-&gt;landType == INDESTRUCTIBLE_BRICK) {
              SDL_QueryTexture(Game_Texture-&gt;Solidblock, NULL, NULL, &amp;dest.w, &amp;dest.h);
              SDL_RenderCopy(Renderer, Game_Texture-&gt;Solidblock, NULL, &amp;dest);
          }
      }
  }
  free(d);
  </code></pre>
  
  <blockquote>
    <p>MAP_SIZE = sizeof(map)</p>
  </blockquote>
  
  <p>I use Visual Studio, when i run debug without breakpoints, I've always got a memory access violation in the first loop turns.</p>
  
  <p>with breakpoints and slowly/constant F5 push, no errors, the loop finnish as well...</p>
  
  <p>I don't understand why this error appears, the 2 loops havent 100 turns in this example, the violation is totally random, sometimes in the 5th turn, sometimes in the 90th...</p>
  
  <p>This sample of code is running in another external loop, and when the first execution works fine, the others never has any violation error.</p>
  
  <h2>UPDATE 1</h2>
  
  <p>Now, i use as well my <code>d</code> variable and exit <code>if malloc() return NULL</code>. The <code>GetAreaPos()</code> return correct value, but the memory violation always appaers on the first if condition at randomly between 2nd and 6th turn of second FOR loop in tests</p>
  
  <p>Header definitions : </p>
  
  <pre><code>#define MAP_SIZE sizeof(map)
  #define AREA_SIZE sizeof(union area)
  #define AREA_RESOLUTION 64
  #define MAP_WIDTH 10//28
  #define MAP_HEIGHT 10//14
  
  //#pragma pack(1)
  typedef enum {
      EMPTY = 00,
      INDESTRUCTIBLE_BRICK = 10,
      DESTRUCTIBLE_BRICK = 11
  } landType;
  
  typedef enum {
      BONUS_BOMB_SCOPE = 000,
      MALUS_BOMB_SCOPE = 001,
      BONUS_BOMB_AMOUNT = 010,
      MALUS_BOMB_AMOUNT = 011,
      BONUS_PLAYER_SPEED = 100,
      MALUS_PLAYER_SPEED = 101,
      NO_BONUS_MALUS = 110,
      NO_MALUS_BONUS = 111,
  } bonusType;
  
  union area {
      struct {
          bool inFire :4;
          landType landType :8;
          bool presenceBomb :4;
          bool presenceBonus :4;
          bonusType typeBonus :12;
      };
      char c;
  };
  //#pragma pack(0)
  
  typedef union area map[MAP_WIDTH * MAP_HEIGHT];
  </code></pre>