title: Setting up and accessing array of void pointers
body: <p>In a block of memory, treatead as two segments, A and B,
  i set up pointers in segment A such that values from
  segment B can be viewed as matrices and accessed by indices:</p>
  
  <pre><code>float **matrix = ( ( void ** ) view-&gt;data[ i ] )[ j ];
  </code></pre>
  
  <p>After allocating memory and assigning the pointers i then set all the
  values for a specific matrix.
  However, when trying to print individual values of that matrix,
  the program seg-faults due to an invalid read.
  If i dont't call the set_weights function, values are printed
  fine ( and valgrind reports no leaks ). So i assume that trying
  to set values has an unwanted side effect of messing up pointers.</p>
  
  <p>I would like to understand if the error is in the pointer assignments
  or on access.
  Pease have a look and help me.</p>
  
  <p>Regards,
  Alfred</p>
  
  <p>[ OS: x86_64 debian linux, gcc 4.2.9 ]</p>
  
  <pre><code>#include &lt;assert.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  enum { Weight, Delta, Max };
  
  typedef struct {
      void **data;
  } View;
  
  void matrix_2D_print( int x, int y, float **values ) {
      int i, j;
      for( i = 0; i &lt; x; ++i ) {
          for( j = 0; j &lt; y; ++j ) {
              printf( "\t\t%f", values[ i ][ j ] ); // Invalid read
              if( j &lt; ( y - 1 ) ) {
                  printf( ", " );
              }
          }
          printf( "\n" );
      }
  }
  
  void set_weights( int layers, int x, int y, View *view ) {
      int i, j, l;
      void **view_ptr = view-&gt;data[ Weight ];
  
      for( l = 0; l &lt; layers; ++l ) {
          float **m2_data = view_ptr[ l ];
  
          for( i = 0; i &lt; x; ++i ) {
              for( j = 0; j &lt; y; ++j ) {
                  m2_data[ i ][ j ] = 0.27f;
              }
          }
      }
  }
  
  int main( int argc, char **argv ) {
      int i, l, m;
      int x = 3;
      int y = 2;
      int layers = 2;
      size_t step_view = Max * sizeof( void ** );
      size_t len_step_view = x * sizeof( void ** );
      size_t len_segment_A = step_view + x * layers * Max * sizeof( void ** );
      size_t len_segment_B = x * y * layers * Max * sizeof( float );
      char *storage = calloc( 1, len_segment_A + len_segment_B );
      float *segment_b = ( float * )( storage + len_segment_A );
      View view;
      view.data = ( void ** ) storage;
  
      for( m = 0; m &lt; Max; ++m ) {
          void **segment_a = ( void ** )( storage + step_view );
          view.data[ m ] = segment_a;
  
          for( l = 0; l &lt; layers; ++l ) {
              segment_a[ l ] = segment_b;
              void **cur = segment_a[ l ];
  
              for( i = 0; i &lt; x; ++i ) {
                  cur[ i ] = segment_b;
                  segment_b += y;
              }
              step_view += len_step_view;
          }
      }
      assert( len_segment_A == step_view );
      set_weights( layers, x, y, &amp;view );
  
      void **view_ptr = view.data[ Weight ];
      printf( "\tLayer: %d\n", 0 );
      matrix_2D_print( x, y, view_ptr[ 0 ] );
  
      free( storage );
      return 0;
  }
  </code></pre>