Я потратил время на расследование, пока я не попытался использовать Malloc () и бесплатно () , чтобы увидеть, что происходит, и да, это тоже. сбои: < /p>
Код: Выделить всё
#include
#include
#include
#include
#include
#include
template
struct IngaAllocator
{
typedef T value_type;
IngaAllocator() {};
~IngaAllocator() {};
template
constexpr IngaAllocator(IngaAllocator const &) noexcept {}
IngaAllocator(IngaAllocator const &){}
T * address(T& x) const {return &x;};
const T * address(const T& x) const {return &x;};
[[nodiscard]] T * allocate(std::size_t n)
{
if(n > std::numeric_limits::max() / sizeof(T))
{
throw std::bad_array_new_length();
}
return static_cast(malloc(sizeof(T) * n));
}
void deallocate(T * p, std::size_t n) noexcept
{
free((void *)p);
}
template
void construct(U* p, Args&&... args) noexcept
{
new(static_cast(p))U(std::forward(args)...);
}
template
void destroy(U* p)
{
p->~U();
}
inline bool operator == (IngaAllocator const &a) {return this == &a;}
inline bool operator != (IngaAllocator const &a) {return !operator == (a);}
};
template
class List : public std::list{};
template
class UnorderedMap : public std::unordered_map
{
};
int main(int argc, char **argv)
{
UnorderedMap map;
map["one"] = 1;
for(auto & it: map)
{
fprintf(stderr, "%s : %d\n", it.first.c_str(), it.second);
}
auto it = map.find("one");
map.erase(it); //NO PROBLEM
for(auto & it: map)
{
fprintf(stderr, "%s : %d\n", it.first.c_str(), it.second);
} //WILL PRINT NOTHING
List l;
l.push_back(1);
l.push_back(2);
for(auto au : l)
{
fprintf(stderr, "%d\n", au);
}
l.remove(2); //CRASHES HERE
for(auto au : l)
{
fprintf(stderr, "%d\n", au);
}
return 0;
}
Код: Выделить всё
free()Что я делаю не так? Для ingaallocator я просто нахожу это во многих местах, где коды в основном идентичны. Так что я полностью потерян о том, почему это сбои, и как это исправить, если возможно.>
Подробнее здесь: https://stackoverflow.com/questions/795 ... ving-items
Мобильная версия