user3280133
user3280133

Reputation: 10699

C++ std::allocator::allocate() giving me a bad pointer

I'm recreating a linked list in C++ and am getting a bad pointer while overloading the += operator. I imagine that I'm just using the allocator in the wrong way, but I could be wrong.

Here is the context:

void MyLinkedList::operator+=( const std::string& s )
{
    allocator<Node> a;
    Node* n = a.allocate(1);

    Node node(s);
    (*n) = node;
    if ( first == NULL )
        first = n;
    else
    {
        (*current).SetNext(n);
        current = n;
    }
}

where first and current are of type Node*. Thanks in advance!

Upvotes: 1

Views: 91

Answers (1)

yizzlez
yizzlez

Reputation: 8805

std::allocator allocates raw unconstructed storage. To use the storage, you must use .construct().

a.construct(n, /* initializer */);

Upvotes: 3

Related Questions