Siluorman
Siluorman

Reputation: 1

Vector in c++ error when trying to call at()

I'm trying to get a pointer to mytype of a vector(vector mytype*) but I get segmegation fault. I've the following code:

void add(string b, vector<mytype*> *p){
    int a;
    mytype *fre=NULL;
    a=oura.front();
    oura.pop();
    if(!(p->size()<a) && oura.size()>0){
        fre=p->at(a-1); //Error seems to come from here
        add(b,fre->get_vec());
    }
    else{ ...}
    }

mytype was a class with a string and a vector<mytype*> pointer.

The error seems to come from fre=p->at(a-1)

oura is a queue<int> and takes values between 1-20 so i used (a-1).

Upvotes: 0

Views: 86

Answers (1)

Paxxi
Paxxi

Reputation: 446

at does bounds checking when accessing elements. If it's out of bounds it will throw an std::out_of_range exception.

adding an assert(a > 0); should let you find out when it happens.

Upvotes: 2

Related Questions