title: trying to copy data with memcpy, getting error "Access violation writing location"
body: <p>I'm getting error on second memcpy<br>
  memcpy(&amp;check_user, &amp;ZZZ, (int)&amp;main - (int)&amp;check_user);</p>
  
  <p>"Unhandled exception at 0x72cc1f57 (msvcr100.dll) in 11.exe: 0xC0000005: Access violation writing location 0x00f31000."</p>
  
  <p>What is wrong?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  #define PASSWD "+++"
  #define MAX_LEN 1023
  #define MAX_CODE_SIZE (0x10 * 1024)
  #define OFFSET_1 0x42
  #define OFFSET_2 0x67
  
  #define x_original_1 0xc01b0574
  #define x_original_2 0x44681574
  #define x_original_all 0x13D4C04B
  
  #define x_crypt 0x66
  using namespace std;
  
  int check_user()
  {
          char passwd[MAX_LEN];
  
          cout&lt;&lt; "enter password:";
          fgets(passwd, MAX_LEN, stdin);
          return ~strcmp(passwd, PASSWD);
  }
  
  int my_func()
  {
          if (check_user())
          {
                  cout&lt;&lt;"passwd ok\n";
          }
          else
          {
                  cout&lt;&lt;"wrong passwd\n";
                  exit(-1);
          }
          return 0;
  }
  
  int main()
  {
          int a, b = 0;
          #pragma pack(1)
          union f
          {
                  char buf[MAX_CODE_SIZE];
                  struct
                  {
                          int local_var_1;
                          int local_var_2;
                          char gag_1[OFFSET_1 - sizeof(int) * 2];
                          int x_val_1;
                          char gag_2[OFFSET_2 - OFFSET_1 - sizeof(int)];
                          int x_val_2;
                  };
          };
    union f ZZZ;
  
       memcpy(&amp;ZZZ, &amp;check_user, (int)&amp;main - (int)&amp;check_user);
  
          for (a = 0; a &lt; (int)&amp;main - (int)&amp;check_user; a++)
          {
                  (*(char *) ((int)&amp;ZZZ + a)) ^= x_crypt;
          }
  
       memcpy(&amp;check_user, &amp;ZZZ, (int)&amp;main - (int)&amp;check_user);
  
          for (a = 0; a &lt; (int)&amp;main - (int)&amp;check_user; a++)
          {
                  b += *(int *)((int)&amp;check_user + a);
          }
          if (b != x_original_all)
          {
                  fprintf(stderr, "-ERR: invalid CRC (%x)\n", b);
                  return 0;
          }
  
  
          my_func();
  
  }
  </code></pre>