[general C++] [solved] std::out_of_range & std::bad_alloc

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

[general C++] [solved] std::out_of_range & std::bad_alloc

Post by Cube_ »

so, I am making use of vectors of vectors to have typesafe 2D arrays.

each vector of vectors is a vector of int vectors, there are five* of them.
if I only use 4 it's fine.
if I use 5? bad things happen.

5 should yield 5(4(32^2)) bytes of data usage + whatever header data vectors have, or 20.48kb.

This, somehow, fails.

Code: Select all

std::vector< std::vector< int > > convertMeta(std::vector< std::vector< int > > input)
{
    std::vector< std::vector< int > > output;
    output.resize(input.size());
    for(int x = 0; x < input.size(); x++)
    {
        output[0].resize(input[0].size());
        for(int y = 0; y < input[0].size(); y++)
        {
            std::cout   << "x: " << x << " y: " << y << " size: "
                        << output.size() << " " << output[0].size() << std::endl;
//SNIP, this is just a switch that makes output.at(x).at(y) either 1 or 0.
//since neither X nor Y ever go past 31 this SHOULD not be a problem, besides the out of range happens for x:1, not x:32.
        }
    }
return output;
}
I use identical code in other places that DOES work fine, and in any case I'm nowhere near memory limits, in fact the program doesn't even use 40 megs of memory.

the debug printing indicates that the range error happens at X: 1 Y: 0 (right after X:0 Y: 31), but this is nonsense.
because it also blatantly shows that X has 32 available slots and that Y has 32 available slots.
output.size() returns 32.
output[0].size() also returns 32, is there some limit I am unaware of? If so, how would one bypass or work around said limit.
I didn't find any relevant data on it.

*plus how many are created when passing them as arguments, also assuming that the functions of type std::vector< std::vector< int > > aren't destructed when their scope ends, which they are/should be.
Last edited by Cube_ on Sun Dec 27, 2015 11:42 pm, edited 1 time in total.
"this is not the bottleneck you are looking for"
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: [general C++] unexpected std::out_of_range & std::bad_al

Post by hendu »

Code: Select all

output[0].resize(input[0].size());
Stare very hard at that line ;)
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: [general C++] unexpected std::out_of_range & std::bad_al

Post by Cube_ »

.... I fail to see the problem.
output[0].resize() resizes the vector contained within the vector.
input[0].size() returns 32, these are of type unsigned int, as is the vector I'm feeding this to.

not to mention I use more or less identical code in another function
vector[row].resize(int y)....

Bloody hell I'm stupid, I should do output[x].resize(y);

thanks for pointing that out, I'd never have found it myself.
"this is not the bottleneck you are looking for"
Post Reply