title: Android NDK: C++ struct member access, causes SIGBUS (signal SIGBUS: illegal alignment)
body: <p>I'm porting a C++ library on Android (Android Studio 2.3.1/NDK 25); the library works flawlessly on UWP (VS2017 VC 1.41 - ARM &amp; Win32) a custom ARM7 board (GCC 4.8).</p>
  
  <p>When debugging on Android Studio, I get a <em>"SIGBUS (signal SIGBUS: illegal alignment)"</em> during an assignment to a struct member.
  Here is the struct (I need it to be 64-bit aligned):</p>
  
  <pre><code>typedef unsigned int        _t_u32;         // 32-bit unsigned
  typedef unsigned long long  _t_u64;         // 64-bit unsigned
  
  typedef struct __attribute__((aligned(8)))
  {
      _t_u32      crc32;
      _t_u64      counter;
  
  } t_security;
  </code></pre>
  
  <p>Now, here is the code snippet:</p>
  
  <pre><code>void prepareBuffer(_t_u8 cmd, _t_u8 *buffer, _t_u32 buffferLen)
  {
      t_security *secPtr = ((t_security *)(buffer + sizeof(_t_u8)));
  
      secPtr-&gt;crc32 = 0;
      secPtr-&gt;counter= 0; &lt;&lt; when this is being executed, on Android Studio-only, I get *"SIGBUS (signal SIGBUS: illegal alignment)"*
      ...
      ...
  }
  </code></pre>
  
  <p>From Android Studio debugger watches:</p>
  
  <pre><code>sizeof(t_security) = {unsigned int} 16
  &amp;secPtr = {t_security * | 0xdc98eb41} 0xdc98eb41
  &amp;secPtr-&gt;crc32 = {_t_u32 * | 0xdc98eb41} 0xdc98eb41
  &amp;secPtr-&gt;counter = {_t_u64 * | 0xdc98eb49} 0xdc98eb49
  </code></pre>
  
  <p>From Visual Studio debugger watches (ARM platform):</p>
  
  <pre><code>sizeof(t_security)  16  unsigned int
  secPtr  0x00afe2e5 {crc32=3435973836 ...}   t_security *
  &amp;secPtr-&gt;crc32  0x00afe2e5 {3435973836} unsigned int *
  &amp;secPtr-&gt;counter    0x00afe2ed {14757395258967641292}   unsigned __int64 *
  </code></pre>
  
  <p>I suppose it has to due to packing/member alignment... but as you can notice, the packaging seems consistent on the two platforms... just on Android Studio-only, I get <em>"SIGBUS (signal SIGBUS: illegal alignment)"</em>.</p>
  
  <p>Can someone please help me understand what's going on? 
  May be a compiler switch I'm missing? Here's the ndk's gradle config:</p>
  
  <pre><code>android.ndk {
      moduleName = "NativeLib"
  
      // add compilation flags
      cppFlags.add("-DANDROID")
      cppFlags.add("-frtti")
      cppFlags.add("-std=c++14")
      cppFlags.add("-fexceptions")
  
      // include headers
      cppFlags.add("-I${file("native-src")}".toString())
  
      ldLibs.addAll("android", "dl", "log", "z", "atomic")
  
      stl = "c++_static"  // LLVM compiler
  }
  android.buildTypes {
      all {
          // To solve struct packing issues, setting abiFilters to package only 32-bit architectures:
          ndk.with {
              abiFilters.add("armeabi")
              abiFilters.add("armeabi-v7a")
              abiFilters.add("mips")
              abiFilters.add("x86")
          }
      }
      debug {
          ndk.with {
              cppFlags.add("-DDEBUG")
              CFlags.add("-DDEBUG")
          }
      }
  }
  </code></pre>
  
  <p>Many thanks!</p>