Crash on big array in C++

3

I am working on OpenCL and I code like this in MS VS 2012:

cl_float test[480000];

It can be complied successfully but the program crashes when it enter the function which the codes above are in. Error Code: 0xC000041D.

It is interrupted near the end of file "chkstk.asm":

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
        test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

        end

On the line test dword ptr [eax],eax ; probe page..

I don't think this array is too big to be allocated.


So next, I want to try another way:

cl_float4 *PixVectIn=(cl_float4*)malloc(480000*sizeof(cl_float4));
for(unsigned int a=0;a<800;a++){
    for(unsigned int b=0;b<600;b++){
        PixVectIn[a*800+b].x=PixVect[a][b][0];
        PixVectIn[a*800+b].y=PixVect[a][b][1];
        PixVectIn[a*800+b].z=PixVect[a][b][2];
    }
}

The program also can be built. This time, it crashes when run the "for" loop.

It stop at PixVectIn[a*800+b].x=PixVect[a][b][0];. It shows me the same error code and at this moment, a=600 and b=252. Looks like it reaches a boundary something.

I'm not sure. It may because I changed certain options in project properties.

c++
asked on Stack Overflow Nov 12, 2015 by Soban • edited Nov 12, 2015 by user703016

3 Answers

3

First you should compare the pointer malloc returned with NULL, otherwise you don't know if the allocation was successful.

Second your indices are wrong: PixVectIn[a*800+b] will go out of the bounds of 480000, since your a alone goes up to 800, and 800^2 is > 480000. Maybe you meant a*b?

answered on Stack Overflow Nov 12, 2015 by Superlokkus
1

Increase your stack size or declare that array as static or place it in global scope. Your stack (in your case) is not big enough to handle 1920000 bytes...

answered on Stack Overflow Nov 12, 2015 by Malkocoglu
1

First program is bitten by stack overflow.

For the second, a*800+b is access violation for values like 799, 800*800 + 600 > 480000.

Also better replace this:

cl_float4 *PixVectIn=(cl_float4*)malloc(480000*sizeof(cl_float4));

with this:

std::vector<cl_float4> PixVectIn(480000);
answered on Stack Overflow Nov 12, 2015 by vladon

User contributions licensed under CC BY-SA 3.0