Boost C++ Libraries: Ticket #3786: Incoherent views of various storage order https://svn.boost.org/trac10/ticket/3786 <p> I have a 256x258 array with only the first 256 lines of interest. The flatten memory block contains then 256 interesting values, 2 craps, 256 interesting values, 2 craps, etc. </p> <p> I want to access each column of the interesting block, then each line. For this I use : </p> <ol><li>a view of the array with limited range in the second dimension </li><li>a view of line-major ordered array reference to the same block of memory, with limited range in the first dimension </li></ol><p> Here is a test code: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&quot;boost/multi_array.hpp&quot;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span> <span class="p">()</span> <span class="p">{</span> <span class="c1">// Create a 2D array that is 256 x 258</span> <span class="k">typedef</span> <span class="n">multi_array</span><span class="o">&lt;</span><span class="kt">float</span><span class="p">,</span> <span class="mi">2</span><span class="o">&gt;</span> <span class="n">array_type</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">multi_array_ref</span><span class="o">&lt;</span><span class="kt">float</span><span class="p">,</span> <span class="mi">2</span><span class="o">&gt;</span> <span class="n">ref_type</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">array_type</span><span class="o">::</span><span class="n">array_view</span><span class="o">&lt;</span><span class="mi">2</span><span class="o">&gt;::</span><span class="n">type</span> <span class="n">view_type</span><span class="p">;</span> <span class="n">array</span><span class="o">&lt;</span><span class="kt">size_t</span><span class="p">,</span> <span class="mi">3</span><span class="o">&gt;</span> <span class="n">paddedExt</span> <span class="o">=</span> <span class="p">{</span><span class="mi">256</span><span class="p">,</span><span class="mi">258</span><span class="p">};</span> <span class="n">array_type</span> <span class="n">A</span><span class="p">(</span><span class="n">paddedExt</span><span class="p">);</span> <span class="cm">/*Initialize the whole memory block with -1 */</span> <span class="n">fill</span><span class="p">(</span><span class="n">A</span><span class="p">.</span><span class="n">origin</span><span class="p">(),</span> <span class="n">A</span><span class="p">.</span><span class="n">origin</span><span class="p">()</span><span class="o">+</span><span class="n">A</span><span class="p">.</span><span class="n">num_elements</span><span class="p">(),</span><span class="o">-</span><span class="mf">1.0</span><span class="p">);</span> <span class="cm">/*Fill the usefull part of the array by accessing data by column*/</span> <span class="n">view_type</span> <span class="n">B</span> <span class="o">=</span> <span class="n">A</span><span class="p">[</span><span class="n">indices</span><span class="p">[</span><span class="n">range</span><span class="p">()][</span><span class="n">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">256</span><span class="p">)]];</span> <span class="k">for</span><span class="p">(</span><span class="n">view_type</span><span class="o">::</span><span class="n">iterator</span> <span class="n">i</span><span class="o">=</span><span class="n">B</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span> <span class="n">i</span><span class="o">!=</span><span class="n">B</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span><span class="p">(</span><span class="n">view_type</span><span class="o">::</span><span class="n">subarray</span><span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;::</span><span class="n">type</span><span class="o">::</span><span class="n">iterator</span> <span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="o">-&gt;</span><span class="n">begin</span><span class="p">();</span> <span class="n">j</span><span class="o">!=</span><span class="n">i</span><span class="o">-&gt;</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">j</span><span class="p">)</span> <span class="o">*</span><span class="n">j</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">;</span> <span class="cm">/*Access usefull data by line*/</span> <span class="n">paddedExt</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">=</span><span class="mi">258</span><span class="p">;</span> <span class="n">paddedExt</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">=</span><span class="mi">256</span><span class="p">;</span> <span class="n">ref_type</span> <span class="n">C</span><span class="p">(</span><span class="n">A</span><span class="p">.</span><span class="n">origin</span><span class="p">(),</span> <span class="n">paddedExt</span><span class="p">,</span> <span class="n">fortran_storage_order</span><span class="p">);</span> <span class="n">view_type</span> <span class="n">D</span> <span class="o">=</span> <span class="n">C</span><span class="p">[</span><span class="n">indices</span><span class="p">[</span><span class="n">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">256</span><span class="p">)][</span><span class="n">range</span><span class="p">()]];</span> <span class="k">for</span><span class="p">(</span><span class="n">view_type</span><span class="o">::</span><span class="n">iterator</span> <span class="n">i</span><span class="o">=</span><span class="n">D</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span> <span class="n">i</span><span class="o">!=</span><span class="n">D</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span><span class="p">(</span><span class="n">view_type</span><span class="o">::</span><span class="n">subarray</span><span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;::</span><span class="n">type</span><span class="o">::</span><span class="n">iterator</span> <span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="o">-&gt;</span><span class="n">begin</span><span class="p">();</span> <span class="n">j</span><span class="o">!=</span><span class="n">i</span><span class="o">-&gt;</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">j</span><span class="p">)</span> <span class="n">output</span><span class="p">(</span><span class="o">*</span><span class="n">j</span><span class="p">);</span> <span class="k">return</span> <span class="n">EXIT_SUCCESS</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> This outputs a pretty high number of -1 ! More precisely, we have : </p> <ul><li>256 values </li><li>2 craps, 254 values </li><li>2 values, 2 craps, 252 values </li><li>4 values, 2 craps, 250 values </li><li>etc. </li><li>the last 2*256 good values are not output </li></ul><p> It's like the views have lost their index_bases somewhere on the way. </p> <p> I compiled and run this test under winXP, mingw32, gcc 3.4.5 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3786 Trac 1.4.3