STD :: LISTE mit einem benutzerdefinierten Allocator stürzt beim Entfernen von Elementen ab
Posted: 17 Apr 2025, 23:44
Ich habe einen Speicher Allocator geschrieben, der mit Standardcontainern verwendet wird. Jedes Mal, wenn ich versuche, ein Element aus einer std :: list zu entfernen, stürzt das Programm in der Zeile L.Remove (ELT) ab. Abstürze: < /p>
SO, auch mit malloc () /, es stürzt ab.
Was mache ich falsch? Für Ingaallocator finde ich dies nur an zahlreichen Orten, an denen die Codes grundsätzlich identisch sind. Ich bin also total verloren, warum es abstürzt und wie man es behebt, wenn möglich.>
Code: Select all
#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;
}
Code: Select all
free()
Was mache ich falsch? Für Ingaallocator finde ich dies nur an zahlreichen Orten, an denen die Codes grundsätzlich identisch sind. Ich bin also total verloren, warum es abstürzt und wie man es behebt, wenn möglich.>