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

查看:134
本文介绍了自定义STL分配器导致第一个字符被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的问题,@BenVoigt的每条建议堆栈分配的字符串流存储,我设计了一个stack_allocator(代码如下),并声明了一个basic_ostringstream类型使用它。



我遇到一个奇怪的错误。
当我打印结果字符串时,我放入流中的第一个字符被省略!



这里是一个例子:

  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<< 你好,世界;
std :: cout<< _os.str()< std :: endl;
return 0;
}

结果输出为:


$ b $


任何人都可以详细说明第一个字符发生了什么?



stack_allocator的实现如下:这是很简单的,我确定有很多改进的空间(无法修复错误!)

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

模板< 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&参考;
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&
struct rebind
{
typedef stack_allocator< U,capacity,arr_size>其他;
};

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 {
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);
}

内联指针地址(引用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
}

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];
};


解决方案

您的 c $ c>如果你分配多于 arr_size 的项目,函数可以结束。如果你使用 g ++ -Wall ,它会警告你这些事情。



另一个问题是,你的 _buf 数组索引向后。它应该是 static T _buf [arr_size] [capacity]; 它具有 arr_size 作为行,



还要注意,只要避免使用以<$ c $开头的标识符c> _ ,因为一些这样的标识符被保留用于实现,并且更容易从不使用它们而不是记住精确的规则。最后,不要直接包含 bits / 头,只需使用真正的头。在这种情况下, memory 。我还必须添加< iostream> < sstream> p>

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.

这篇关于自定义STL分配器导致第一个字符被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆