jeudi 22 septembre 2016

Why Am I Getting std::length_error: vector Exception?

In this code

Constraint cs('r', v, c);
std::cout << "current size " << current.overlaps.size() << std::endl;
std::cout << "    max size " << current.overlaps.max_size() << std::endl;
current.overlaps.push_back(cs);

I am getting the message

libc++abi.dylib: terminating with uncaught exception of type std::length_error: vector

Constraint is a very simple struct:

struct Constraint {
   char side;  // will be t, b, l, or r for top, etc.
   int value;  // required value for the side
   size_t count;  // how many holes match this constraint?
   Constraint(char s, int v, size_t c) : side(s), value(v), count(c) {}
   Constraint() = default;
   <...inconsequential methods...> 
};

current is an instance of Current:

template<int N>
struct Current {
    int row; // row and column filled at this level
    int col;
    std::vector<Constraint> overlaps;
    std::vector<Hole*>::iterator candidates;
    std::vector<Hole*>::iterator stop;
    Hole* choice; // hole used at this level
    Current() = default;
};

At the point where I am getting the error, I expect overlaps to be a vector of size 0, which will expand as I push Constraints. In the debug navigator (in Xcode 7.2.1 using clang and c++11) I see

overlaps    std::__1::vector<Constraint, std::__1::allocator<Constraint> >  size=0  

which is just what I expect (except for the ::__1, which I don't get.) However, the output from the debugging print statements is

current size 18446744073709551609
    max size 1152921504606846975

and indeed, when I follow the code in the debugger the exception is thrown from <vector> in the _recommend method (called from push_back) which tries to resize to 18446744073709551610.

I'm sure I'm making some simple mistake, but the conflict between the information in the debug navigator and the print output has got me flummoxed. I apologize for not posting a MCVE, but the code to get to this point in the program is quite complex, and it would would take a fair amount of time to construct a MCVE. I'm hoping that the mistake is so glaring that you will be able to put me on the right track from what I've posted.

Aucun commentaire:

Enregistrer un commentaire