自定义STL分配器导致删除第一个字符[英] custom STL allocator causes first character to be dropped

本文是小编为大家收集整理的关于自定义STL分配器导致删除第一个字符的处理方法,想解了自定义STL分配器导致删除第一个字符的问题怎么解决?自定义STL分配器导致删除第一个字符问题的解决办法?自定义STL分配器导致删除第一个字符问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

根据@benvoigt的建议,以响应我的

最终的输出为:

Ello World

任何人都可以详细说明第一个字符发生的事情吗?

stack_allocator inpm inspon:这很简单,我敢肯定有很大的改进空间(尽管没有解决该错误!)

)

#include <cstddef>
#include <limits>
#include <bits/allocator.h>

template<typename T, size_t capacity = 1024, size_t arr_size = 5>
class stack_allocator 
{
 public: 
  typedef T value_type;
  typedef value_type* pointer;
  typedef const value_type* const_pointer;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  inline explicit stack_allocator() { }
  template<typename U>
  inline explicit stack_allocator(const stack_allocator<U, capacity, arr_size>& that) { }
  inline ~stack_allocator() {}

  template<typename U>
  struct rebind 
  {
    typedef stack_allocator<U, capacity, arr_size> other;
  };

  inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) 
  {
    if (cnt > capacity)
      return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T))); 
    for (size_t i = 0; i < arr_size; ++i)
    {
      if (!_used[i])
      {
        _used[i] = true;
        return reinterpret_cast<pointer>(_buf[i]); 
      }
    }
  }

  inline void deallocate(pointer p, size_type) 
  {
    for (size_t i = 0; i < arr_size; ++i) 
    {
      if (p != _buf[i])
        continue;
      _used[i] = false;
      return;
    }
    ::operator delete(p); 
  }

  inline pointer address(reference r) { return &r; }
  inline const_pointer address(const_reference r) { return &r; }

  inline size_type max_size() const 
  { 
    return std::numeric_limits<size_type>::max() / sizeof(T);
  }

  inline void construct(pointer p, const T& t) { new(p) T(t); }
  inline void destroy(pointer p) { p->~T(); }

  inline bool operator==(const stack_allocator&) const { return true; }
  inline bool operator!=(const stack_allocator& a) const { return !operator==(a); }

 private:
  static __thread bool _used[arr_size];
  static __thread T    _buf[capacity][arr_size];
};

推荐答案

如果您分配超过arr_size项目,则您的allocate功能可能会掉落.如果您使用g++ -Wall,它将警告您有关这些事情的信息.

另一个问题是您的_buf数组索引是向后的.它应该是static T _buf[arr_size][capacity];,它具有arr_size作为行,而不是您在原始代码中拥有的订单,它使容量成为第一个索引.

另外,作为旁注,只需避免从领导_开始的标识符,因为某些此类标识符是为实施而保留的,而且从不使用它们比记住精确规则更容易.最后,切勿直接使用bits/标题,只需使用真实的标题即可.在这种情况下,memory.我还必须添加<iostream>和<sstream>才能使其进行编译.

本文地址:https://www.itbaoku.cn/post/486780.html

问题描述

Per suggestion from @BenVoigt in response to my question regarding stack allocated stringstream storage, I designed a stack_allocator (code follows below), and declared a basic_ostringstream type using it.

I am experiencing a strange bug though. The first character I place into the stream is omitted when I print the resulting string!

Here is an example:

template<typename T, size_t capacity, size_t arr_size>
__thread bool stack_allocator<T, capacity, arr_size>::_used[arr_size] = {};

template<typename T, size_t capacity, size_t arr_size>
__thread T stack_allocator<T, capacity, arr_size>::_buf[capacity][arr_size] = {};

typedef std::basic_ostringstream<char, 
                                 std::char_traits<char>, 
                                 stack_allocator<char, 1024, 5> > stack_ostringstream;  
int main()
{
  stack_ostringstream _os;
  _os << "hello world";
  std::cout << _os.str() << std::endl;
  return 0;
}

The resulting output is:

ello world

Can anyone elaborate on what is happening to the first character?

The stack_allocator impl follows: It's pretty simplistic, and I'm sure has lots of room for improvement (not withstanding fixing the bug!)

#include <cstddef>
#include <limits>
#include <bits/allocator.h>

template<typename T, size_t capacity = 1024, size_t arr_size = 5>
class stack_allocator 
{
 public: 
  typedef T value_type;
  typedef value_type* pointer;
  typedef const value_type* const_pointer;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  inline explicit stack_allocator() { }
  template<typename U>
  inline explicit stack_allocator(const stack_allocator<U, capacity, arr_size>& that) { }
  inline ~stack_allocator() {}

  template<typename U>
  struct rebind 
  {
    typedef stack_allocator<U, capacity, arr_size> other;
  };

  inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) 
  {
    if (cnt > capacity)
      return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T))); 
    for (size_t i = 0; i < arr_size; ++i)
    {
      if (!_used[i])
      {
        _used[i] = true;
        return reinterpret_cast<pointer>(_buf[i]); 
      }
    }
  }

  inline void deallocate(pointer p, size_type) 
  {
    for (size_t i = 0; i < arr_size; ++i) 
    {
      if (p != _buf[i])
        continue;
      _used[i] = false;
      return;
    }
    ::operator delete(p); 
  }

  inline pointer address(reference r) { return &r; }
  inline const_pointer address(const_reference r) { return &r; }

  inline size_type max_size() const 
  { 
    return std::numeric_limits<size_type>::max() / sizeof(T);
  }

  inline void construct(pointer p, const T& t) { new(p) T(t); }
  inline void destroy(pointer p) { p->~T(); }

  inline bool operator==(const stack_allocator&) const { return true; }
  inline bool operator!=(const stack_allocator& a) const { return !operator==(a); }

 private:
  static __thread bool _used[arr_size];
  static __thread T    _buf[capacity][arr_size];
};

推荐答案

Your allocate function can fall off the end if you allocate more than arr_size items. If you use g++ -Wall it will warn you about those sorts of things.

The other problem is that your _buf array indexes are backwards. It should be static T _buf[arr_size][capacity]; which has the arr_size as the row, not the other order that you have it in the original code which makes the capacity be the first index.

Also as a side note, just avoid identifiers that start with leading _ because some such identifiers are reserved for the implementation and it's easier to never use them than to remember the precise rules. Finally, never include the bits/ headers directly, just use the real headers. In this case, memory. I also had to add includes for <iostream> and <sstream> to get it to compile.

查看更多