Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom STL allocator causes first character to be dropped
    primarykey
    data
    text
    <p>Per suggestion from @BenVoigt in response to my <a href="https://stackoverflow.com/questions/8620383/std-compliant-stringstream-using-stack-allocated-storage/8648341#8648341">question regarding stack allocated stringstream storage</a>, I designed a stack_allocator (code follows below), and declared a basic_ostringstream type using it.</p> <p>I am experiencing a strange bug though. The first character I place into the stream is omitted when I print the resulting string!</p> <p>Here is an example:</p> <pre><code>template&lt;typename T, size_t capacity, size_t arr_size&gt; __thread bool stack_allocator&lt;T, capacity, arr_size&gt;::_used[arr_size] = {}; template&lt;typename T, size_t capacity, size_t arr_size&gt; __thread T stack_allocator&lt;T, capacity, arr_size&gt;::_buf[capacity][arr_size] = {}; typedef std::basic_ostringstream&lt;char, std::char_traits&lt;char&gt;, stack_allocator&lt;char, 1024, 5&gt; &gt; stack_ostringstream; int main() { stack_ostringstream _os; _os &lt;&lt; "hello world"; std::cout &lt;&lt; _os.str() &lt;&lt; std::endl; return 0; } </code></pre> <p>The resulting output is:</p> <blockquote> <p>ello world</p> </blockquote> <p>Can anyone elaborate on what is happening to the first character?</p> <p>The stack_allocator impl follows: It's pretty simplistic, and I'm sure has lots of room for improvement (not withstanding fixing the bug!)</p> <pre><code>#include &lt;cstddef&gt; #include &lt;limits&gt; #include &lt;bits/allocator.h&gt; template&lt;typename T, size_t capacity = 1024, size_t arr_size = 5&gt; class stack_allocator { public: typedef T value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type&amp; reference; typedef const value_type&amp; const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; inline explicit stack_allocator() { } template&lt;typename U&gt; inline explicit stack_allocator(const stack_allocator&lt;U, capacity, arr_size&gt;&amp; that) { } inline ~stack_allocator() {} template&lt;typename U&gt; struct rebind { typedef stack_allocator&lt;U, capacity, arr_size&gt; other; }; inline pointer allocate(size_type cnt, typename std::allocator&lt;void&gt;::const_pointer = 0) { if (cnt &gt; capacity) return reinterpret_cast&lt;pointer&gt;(::operator new(cnt * sizeof (T))); for (size_t i = 0; i &lt; arr_size; ++i) { if (!_used[i]) { _used[i] = true; return reinterpret_cast&lt;pointer&gt;(_buf[i]); } } } inline void deallocate(pointer p, size_type) { for (size_t i = 0; i &lt; arr_size; ++i) { if (p != _buf[i]) continue; _used[i] = false; return; } ::operator delete(p); } inline pointer address(reference r) { return &amp;r; } inline const_pointer address(const_reference r) { return &amp;r; } inline size_type max_size() const { return std::numeric_limits&lt;size_type&gt;::max() / sizeof(T); } inline void construct(pointer p, const T&amp; t) { new(p) T(t); } inline void destroy(pointer p) { p-&gt;~T(); } inline bool operator==(const stack_allocator&amp;) const { return true; } inline bool operator!=(const stack_allocator&amp; a) const { return !operator==(a); } private: static __thread bool _used[arr_size]; static __thread T _buf[capacity][arr_size]; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload