Exception: std::bad_alloc at memory location ... with SparseMat
int mat_size_o = 60 * 60 * 60;
int sz[] = { 10000, mat_size_o };
SparseMat mem1(2, sz, CV_8UC1);
int idx[2];
for (int i = 0; i < 60; ++i) {
for (int j = 0; j < 60; ++j) {
for (int k = 0; k < 60; ++k) {
int IOut = 60 * 60 * i + 60 * j + k;
Out = calc_sum(IOut);
Outp.at<float>(i, j, k) = Out;
}
}
}
float calc_sum(int IOut) {
int Sum0 = 0;
float Sum = 0;
int IIn0;
int IIn;
uchar Char;
for (int i = 0; i < nrows_i; ++i) {
IIn0 = ncols_i * i;
for (int j = 0; j < ncols_i; ++j) {
IIn = IIn0 + j;
idx[0] = IIn;
idx[1] = IOut;
Char = mem1.ref<uchar>(idx);
Sum0 += Char;
}
}
Sum = Sum0;
return Sum;
}
Generates exception at i=0, j=4, k=59; For another matrix at i=0, j=6, k=59 It happens inside calc_sum() at Char = mem1.ref<uchar>(idx); when i = 43, j = 3 (nrows_i = ncols_i = 100). Initially I mixed int with unsigned int sometimes, but correction doesn't help. Did anybody encounter such errors?
Not really an answer... but the only time I've ever seen bad_alloc is when memory is running low.
Useful hint, indeed. Especially if I use debugger and run several programs, but the error remains even if I exit Visual Studio and run standalone EXE file. Probably this is something similar to General Protection Fault (access violation)? This may happen during type mixing or errors in addressing mode.
This answer also suggests that the cause is memory shortage rather than access violation.
Also using SparseMat drastically reduces speed of calculations.