title: What does "Misaligned address error" mean?
body: <p>First of all — sorry for the specifics. I generally try to boil my SO questions to generic "class A" stuff with only relevant stuff, but I'm not sure what's the source of the problem here.</p>
  
  <p>I have a matrix class template that looks like this (only showing what I think are the relevant parts):</p>
  
  <pre><code>template &lt;std::size_t R, std::size_t C&gt;
  class Matrix
  {
  private:
      //const int rows, cols;
      std::array&lt;std::array&lt;float,C&gt;,R&gt; m;
  public:
      inline std::array&lt;float,C&gt;&amp; operator[](const int i)
      {
          return m[i];
      }
  
      const std::array&lt;float,C&gt; operator[](const int i) const
      {
          return m[i];
      }
  
      template&lt;std::size_t N&gt;
      Matrix&lt;R,N&gt; operator *(const Matrix&lt;C,N&gt; a) const
      {
          Matrix&lt;R,N&gt; result = Matrix&lt;R,N&gt;();
          // irrelevant calculation
          return result;
      }
      // ... other very similar stuff, I'm not sure that it's relevant
  }
  
  template &lt;std::size_t S&gt;
  Matrix&lt;S,S&gt; identity()
  {
      Matrix&lt;S,S&gt; matrix = Matrix&lt;S,S&gt;();
  
      for(std::size_t x = 0; x &lt; S; x++)
      {
          for(std::size_t y = 0; y &lt; S; y++)
          {
              if (x == y)
              {
                  matrix[x][y] = 1.f;
              }
          }
      }
  
      return matrix;
  }
  </code></pre>
  
  <p>I unit tested the whole class, both multiplication and identity factory seem to be working alright. However, then I use it in this method, which gets called <strong>a lot</strong> of times (I think that if you ever wrote a renderer, it's pretty obvious what I'm trying to do here):</p>
  
  <pre><code>Vec3i Renderer::world_to_screen_space(Vec3f v)
  {
      Matrix&lt;4,1&gt; vm = v2m(v);
      Matrix&lt;4,4&gt; projection = identity&lt;4&gt;(); // If I change this to Matrix&lt;4,4&gt;(), the error doesn't happen
      projection[3][2] = -1.f;
      vm = projection * vm;
      Vec3f r = m2v(vm);
      return Vec3i(
              (r.x + 1.) * (width / 2.),
              (r.y + 1.) * (height / 2.),
              r.z
          );
  }
  </code></pre>
  
  <p>And after some amount of time and some amount of random calls to this method, I get this:</p>
  
  <pre><code>Job 1, 'and ./bin/main' terminated by signal SIGBUS (Misaligned address error)
  </code></pre>
  
  <p>However, if I change the line <code>identity&lt;4&gt;()</code> to <code>Matrix&lt;4,4&gt;()</code> the error doesn't happen. I'm new to C++, so it must be something really stupid.</p>
  
  <p>So, (1) what does this error mean and (2) how did I manage to shoot myself in the leg?</p>
  
  <p><strong>Update</strong>: and of course, this bug won't reproduce in the LLDB debugger.</p>
  
  <p><strong>Update 2</strong>: here's what I got after running the program through Valgrind:</p>
  
  <pre><code>==66525== Invalid read of size 4
  ==66525==    at 0x1000148D5: Renderer::draw_triangle(Vec3&lt;float&gt;, Vec3&lt;float&gt;, Vec3&lt;float&gt;, Vec2&lt;int&gt;, Vec2&lt;int&gt;, Vec2&lt;int&gt;, Model, float) (in ./bin/main)
  </code></pre>
  
  <p>And <code>draw_triangle</code> is exactly the method that calls <code>world_to_screen_space</code> and uses it's result.</p>
  
  <p><strong>Update 3</strong>: I discovered the source of the problem, and it wasn't anything related to this code — and it was something pretty obvious, too. Really not sure what to do about this question now.</p>