0

I've posted a similar question to here:

why it crashes when assigning new values to arrays?

But this time, I encountered another problem with arrays again. My code is like this:

double diff[600][800][3];
cv::Mat value ( height, width, CV_8UC3 );
double mean[600][800][3];
....
for ( int i =0; i < 5; ++ i )
{
    for ( int j = 0; j < 3; ++j )
    {
        for ( int m = 0; m < 2; ++m )
        {
            mean[i][j][m] = 10/m;
            diff[i][j][m] = abs ( value.data[value.cols*i+j]-mean[i][j][m]);
        }
    }
}

It crashed at the line:

diff[i][j][m] = abs ( value.data[value.cols*i+j]-mean[i][j][m]);

I even omitted that line, and just wrote it like:

double test = abs ( value.data[value.cols*i+j]-mean[i][j][m]);

and it compiled, then I added

std::cout << test << std::endl;

it again crashed, and this line also crashed:

diff[i][j][m] = test; 

I even changed it like this:

double test = static_cast<double>(abs(value.data[value.cols*i+j]-mean[i][j][m]));
diff[i][j][m] = test;

again, crashed. If I write it like:

diff[i][j][m] = 10; 

it compiles. I really cannot figure out the problem. Seems like this is not related to stack overflowing, or datatype. I even tried Aki Suihkonen's answer for my previous question, but it couldn't help. Could someone help me find the problem here??

Community
  • 1
  • 1
E_learner
  • 3,264
  • 13
  • 52
  • 87
  • 2
    Almost 3 million doubles on the stack. Poor stack. – chris Oct 06 '12 at 09:27
  • What size is value.data array ? I think you are trying to access some memory you are not allowed to. – tomahh Oct 06 '12 at 09:34
  • @Tom: the size of value is 600x800. – E_learner Oct 06 '12 at 09:35
  • @chris: could you provide any suggestion to improve it? – E_learner Oct 06 '12 at 09:36
  • @ederman, Sure, use `std::vector`. The stack implementations I've seen are generally around 1MB. The heap's much bigger. Being 3D, if you need more efficiency, wrap an underlying 1D vector. – chris Oct 06 '12 at 09:38
  • @chris: any example code please, or else mostly I will write it wrong again, and ask again :( I am not very good at writing this. – E_learner Oct 06 '12 at 09:39
  • @chris: by the way, how did you calculate almost 3 million? I think that is the part I need to learn. – E_learner Oct 06 '12 at 09:40
  • 2
    @ederman, For the most part, vectors are like arrays, though a bit more annoying to declare in more than one dimension. There's tons of stuff on vectors out there. They don't perform too well past one dimension either (hence a wrapper). As for almost 3 million, you have 2 arrays of 600*800*3=1440000 doubles each. – chris Oct 06 '12 at 09:45
  • @chris: ok, thank you for your answer. I want to select yours as best answer, but it is in the comments part :( – E_learner Oct 06 '12 at 10:51
  • @chris: I tried using miltidimensional vectors, but it is too much slow to me. Also, it crashed after several seconds of running. Isn't there any other way to improve this? – E_learner Oct 06 '12 at 12:13
  • @ederman, Yes, maybe you forgot to change the division by 0 when using vectors. A debugger would be a good start to figuring out where and why the crash happens. As for multidimensional vector performance, you can make yourself a `Matrix3D` class which contains a 1D vector of length `dimA * dimB * dimC` and make it behave as if it was 3D (an easy way is to use `operator()` to provide `diff(5, 3, 1)` accessing functionality). – chris Oct 06 '12 at 15:50

2 Answers2

2
    for ( int m = 0; m < 2; ++m )
    {
       mean[i][j][m] = 10/m;
       //...
     }

This leads to division by zero for value of m =0 .. Possibly the reason of crash.

bhuwansahni
  • 1,784
  • 1
  • 11
  • 20
  • @bhuwanshni: I don't think so, even I tried giving a constant value like "25", it also crashed. Do you have any other idea? – E_learner Oct 06 '12 at 12:14
1

Probably double diff[600][800][3]; is too big for your system's default stack size. If you have 64-bit doubles then this is 11.5MB, whereas many compilers default to a 1MB stack size.

Try static double diff[600][800][3]; instead , and the same for your other large array.

If you really need the arrays to be non-static (i.e. if the function is re-entrant) you will need to use dynamic allocation.

M.M
  • 134,614
  • 21
  • 188
  • 335