具有嵌入式竞技场的堆栈分配器问题

2024-01-26

我在使用 Howard Hinnant 时遇到崩溃基于堆栈的分配器 http://howardhinnant.github.io/stack_alloc.h,都在 MacOS 和 64 位 Linux 上的 Clang 3.4。这是一个最小的例子 在容器的析构函数中触发崩溃:

#include <vector>
#include "stack_alloc.h"

template <template <typename...> class C, typename T, size_t N>
using stack_container = C<T, stack_alloc<T, N>>;

using stack_vector = stack_container<std::vector, size_t, 4>;

int main()
{
  auto s = stack_vector{1, 2, 3};
  auto m = std::move(s);
}

编译如下:

clang++ -std=c++11 -stdlib=libc++ -g -Wall crash.cc && ./a.out

您知道为什么会发生这次事故吗?我也尝试过 重新实现stack_alloc就竞技场实施而言短分配 http://howardhinnant.github.io/short_alloc.h,但在移动基于堆栈的容器时我仍然遇到崩溃。

这是一个 Linux 回溯:

#0  _int_free (av=0x394f5b8760 <main_arena>, p=0x7fffffffe0f0, have_lock=0) at malloc.c:3901
#1  0x00000000004013eb in stack_alloc<unsigned long, 4ul>::deallocate (this=0x7fffffffe080, p=0x7fffffffe100, n=3)
    at ./stack_alloc.h:71
#2  0x0000000000401343 in capacity (this=0x7fffffffe060, this=0x7fffffffe060, __a=..., __p=0x7fffffffe100, __n=3)
    at .../include/c++/v1/memory:1443
#3  std::__1::__vector_base<unsigned long, stack_alloc<unsigned long, 4> >::~__vector_base (this=0x7fffffffe060)
    at .../include/c++/v1/vector:476
#4  0x0000000000400fa5 in std::__1::vector<unsigned long, stack_alloc<unsigned long, 4> >::~vector (this=0x7fffffffe060)
    at .../include/c++/v1/vector:481
#5  0x0000000000400f6e in main () at crash.cc:13

我有兴趣(i)如果人们可以重现该错误,并且(ii)如何修复它。


您正在使用不符合标准的 C++11stack_alloc。正如欣南特他自己写的 http://howardhinnant.github.io/stack_alloc.html,

我使用完全 C++11 的新分配器更新了本文 符合。它所取代的分配器不完全是 C++03 也不完全是 C++11 一致,因为副本不相等。

更正后的版本名为short_alloc and is 在这里找到 http://howardhinnant.github.io/short_alloc.h.

使用要求将堆栈缓冲区放置在分配器之外 (using 欣南特符号 http://howardhinnant.github.io/stack_alloc.html):

int main()
{
  arena<N> a; // change N to required space
  auto s = SmallVector({1, 2, 3}, Alloc{a});
  auto m = std::move(s);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有嵌入式竞技场的堆栈分配器问题 的相关文章

随机推荐