C++ "new T[size]" doesn't work?
<p>I have a template class defined as:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;queue&gt;

using namespace std;

template&lt;class T&gt;
class tbufferpool {
private:
    const int m_initial;
    const int m_size;
    const int m_total;
    T *m_buffer;
    vector&lt;T*&gt; m_queue;

public:
    // constructor
    tbufferpool(int initial, int size) : m_initial(initial), m_size(size), m_total(initial*size) {
        m_buffer = new T[m_total];
        T* next_buffer = m_buffer;
        for (int i = 0; i &lt; initial; ++i, next_buffer += size) {
            m_queue.push_back(next_buffer);
        }
    }
</code></pre>

<p>and at some point in the constructor I do:</p>

<pre><code>m_buffer = new T[size];
</code></pre>

<p>This works for most use-cases but in one test I get the following memory error reported by valgrind (command and relevant snippet below) the test still passes fine though. The interesting bit is <code>operator new(unsigned long)</code> meaning it is not allocating and aligning for the concrete T type I setup "double" as I expected but for <code>unsigned long</code>? If I modify my bufferpool implementation and hard-code <code>new double[size]</code> then this memory error doesn't show but of course I only work with <code>tbufferpool&lt;double&gt;</code> now. </p>

<p>Can anyone advice how to fix this? the <code>new T[size]</code> should be legal right? since the template parameters is applied at compile time by the pre-processor that creates a new class for each template type used. Would this be a compiler bug?</p>

<p>The test_matrix is a suite containing 30 test cases. Only one test produces the problem shown below in valgrind, that test passes nevertheless. I checked all the inputs to the function call where the problem originates using the <code>new T[size]</code> variant and printed them alongside the same inputs using <code>new double[size]</code> variant. I compare them using AraxisMerge and they are identical. I'm afraid is a problem related to the memory alignment turning out different depending whether I use the template parameter or the concrete double type ... ?</p>

<pre><code>$ valgrind --show-reachable=yes --dsymutil=yes --track-origins=yes ./test_matrix
  [snip]
  ==3719== Conditional jump or move depends on uninitialised value(s)
  ==3719==    at 0x3BE86C8: mkl_blas_dscal (in /opt/intel/composerxe-2011.4.184/mkl/lib/libmkl_mc3.dylib)
  ==3719==    by 0x432FFFFFFFFFFFFF: ???
  ==3719==  Uninitialised value was created by a heap allocation
  ==3719==    at 0xD62F: malloc (vg_replace_malloc.c:266)
  ==3719==    by 0x97B15C: operator new(unsigned long) (in /opt/local/lib/gcc46/libstdc++.6.dylib)
  ==3719==    by 0x7FFF5FBFE54F: ???
  ==3719==    by 0x10014BDBF: ???
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x97B288: operator new[](unsigned long) (in /opt/local/lib/gcc46/libstdc++.6.dylib)
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x100013853: tbufferpool&lt;double&gt;::tbufferpool(int, int) (bufferpool.h:30)
  ==3719==    by 0x7003FFFFF: ???
  ==3719==    by 0x100079E7F: ??? (in ./test_matrix)
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x10014BE0F: ???
  ==3719== 
  ==3719== Conditional jump or move depends on uninitialised value(s)
  ==3719==    at 0x3BE86CA: mkl_blas_dscal (in /opt/intel/composerxe-2011.4.184/mkl/lib/libmkl_mc3.dylib)
  ==3719==    by 0x432FFFFFFFFFFFFF: ???
  ==3719==  Uninitialised value was created by a heap allocation
  ==3719==    at 0xD62F: malloc (vg_replace_malloc.c:266)
  ==3719==    by 0x97B15C: operator new(unsigned long) (in /opt/local/lib/gcc46/libstdc++.6.dylib)
  ==3719==    by 0x7FFF5FBFE54F: ???
  ==3719==    by 0x10014BDBF: ???
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x97B288: operator new[](unsigned long) (in /opt/local/lib/gcc46/libstdc++.6.dylib)
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x100013853: tbufferpool&lt;double&gt;::tbufferpool(int, int) (bufferpool.h:30)
  ==3719==    by 0x7003FFFFF: ???
  ==3719==    by 0x100079E7F: ??? (in ./test_matrix)
  ==3719==    by 0x7FFF5FBFE58F: ???
  ==3719==    by 0x10014BE0F: ???
  [snip]
</code></pre>

<p>System details:</p>

<pre><code>/Users/bravegag/code/fastcode_project/build_debug$ uname -a &amp;&amp; g++ --version
Darwin Macintosh-4.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; 
root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64
g++ (GCC) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code></pre>
