title: `pragma pack(push, 1)` crashes in GCC 4.4.7. Possible compiler bug?
body: <p>I'm encountering a bug that has me stumped.  I've narrowed it down to an issue with the <code>pragma pack</code> command in GCC (specifically RHEL Linux, GCC v.4.4.7) that can be recreated in the small sample case I've shown below.  It looks like GCC is computing the wrong offset in this case, which will manifest itself as a crash within the loop.  Removing the pragma pack also removes the fault - but in the real application this will cause many additional gigabytes of memory use and is not desirable.  </p>
  
  <p>In the example below, you will need to compile with optimizations enabled (O3) to experience the failure.  I've also provided an example item (cMagic) in the structure that can be removed which will change the structure alignment and keep the bug from triggering. </p>
  
  <p>I've taken a look at the generated assembly and believe this may be a compiler bug.  Am I missing something else?  Can anyone confirm this bug or provide any insights? </p>
  
  <p><strong>Crash.cpp:</strong></p>
  
  <pre><code>/*  Platform Version Info:
   *     gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
   *     uname: 2.6.32-504.16.2.el6.x86_64 #1 SMP Tue Mar 10 17:01:00 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
   *
   *  Compiling:
   *     Must use -O3 for compiling and linking
   *     CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
   *     CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
   *
   *  Notes:
   *     This appears to be an optimization and alignment issue.
   *     Getting rid of a byte in Place (cMagic) causes the program to complete successfully.
   *
   */
  
  
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  #pragma pack(push,1)  // Structures must be packed tightly
  #define MAGICCONSTANT 17
  
  struct Place {
     int iFoo;
     char cMagic;         // GCC doesn't like cMagic.  Disillusion it and everything is OK
     int aiArray[MAGICCONSTANT];
  };
  
  
  #pragma pack(pop)
  
  int main(int argc, const char *argv[])
  {
     Place *pPlace = new Place;   // Place must be on the heap... so new, calloc, malloc, etc
  
     for (int c = 0; (c &lt; MAGICCONSTANT); c++) {
        pPlace-&gt;aiArray[c] = 0;
     }
  
     delete pPlace;
  
     cout &lt;&lt; "Complete!" &lt;&lt; endl;
     return 0;
  }
  </code></pre>
  
  <p><strong>Makefile:</strong></p>
  
  <pre><code>CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
  CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
  
  OBJS=   Crash.o
  SRCS=   Crash.cpp
  TARG=   crash
  
  debug:: ${TARG}
  
  all:: ${TARG}
  
  ${TARG}: ${OBJS}
          ${CPP} -o ${TARG} ${OBJS} ${LDFLAGS} ${LIBS}
  
  clean::
          rm -f ${TARG} ${OBJS} ${TARG}.core core
  </code></pre>
  
  <p><strong>Disassembly Graph (Generated ASM Code):</strong></p>
  
  <p><a href="https://i.stack.imgur.com/6sHPm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6sHPm.png" alt="Disassembly graph"></a></p>