c++ - Qt QVector allocation via resize() results in bad_alloc where reserve() seems to work -
so want allocate 1gb of data in qvector. far use this:
void foo (float* data, size_t size) { qvector<float> resultvec; resultvec.resize(size); //i stuff on vector size_t paddinglength = nextpow2(size); //calculates next highest power 2 two //the paddinglength in case @ moment: 268.435.456 // results in bad_alloc resultvec.resize(paddinglength); }
so in case of floats want reserve 1gb worth of space. should not problem @ all. still have 11gb on ram left. tried @ point in program , works fine, sizes in order of 4gb.
my first guess don't have enough continuous space left, when try same reserving space before resizing it, works.
void foo (float* data, size_t size) { qvector<float> resultvec; resultvec.resize(size); //i stuff on vector size_t paddinglength = nextpow2(size); //calculates next highest power 2 two //the paddinglength in case @ moment: 268.435.456 //this works tho resultvec.reserve(paddinglength); resultvec.resize(paddinglength); } template<typename t> t nextpow2 (const t val) { t result = val; (size_t = 1; < sizeof(t) * char_bit; *= 2) { result |= result >> i; } return result + 1; }
fyi: did check return value of next2pow , sure smaller limit of int. shown in comments value bit on 268 million elements, int has maximum of on 2 billion, should safe there. included next2pow implementation above.
am doing wrong, or behaviour expected?
Comments
Post a Comment