amorimluc
amorimluc

Reputation: 1719

Why am I getting a std::bad_alloc error when trying to push to a vector?

C++ Here is the portion of my code that throws the error:

IDlist->push_back(lex->getCurrentToken());

IDList is a vector that is defined like this:

std::vector<Token*>* IDlist;

Why can't that line of code push my Token object? Thanks.

EDIT:

When I try this:

Token* t = lex->getCurrentToken();
IDlist->push_back(t);

I get the same error; it happens when a push into the vector is attempted.

Upvotes: 0

Views: 1335

Answers (1)

billz
billz

Reputation: 45420

std::vector<Token*>* IDlist;

IDlist is a pointer which points to a vector and you haven't allocated it by new. You need to allocate IDlist before using it:

IDlist = new  std::vector<Token*>();

But what's the point of using a pointer to vector? Just declare IDlist as variable:

std::vector<Token*> IDlist;
Token* t = lex->getCurrentToken();
IDlist.push_back(t);

Upvotes: 2

Related Questions