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.

user703016
  • 37,307
  • 8
  • 87
  • 112
Soban
  • 81
  • 5
  • 2
    For the first, 1MB is a typical stack size limit, and the array is larger than that. For the second, 600*800 + 252 is greater than 480000. – user253751 Nov 12 '15 at 07:44
  • [Is there a max array length limit in C++?](http://stackoverflow.com/a/216731/14065) – Martin York Nov 12 '15 at 10:44

3 Answers3

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?

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
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...

Malkocoglu
  • 2,522
  • 2
  • 26
  • 32
  • 1
    Even better, the array should be a (dynamically allocated) heap data. For example, by using `std::vector` – Basile Starynkevitch Nov 12 '15 at 07:55
  • @Basile: Your suggestion is good. I do not know if OpenCL can work with std:vector or similar. Any info ? – Malkocoglu Nov 12 '15 at 07:57
  • 1
    Read more about C++11 [std::vector](http://www.cplusplus.com/reference/vector/vector/). You can use its `data` member function to get a pointer. You might want to upgrade your compiler to something C++11 compliant – Basile Starynkevitch Nov 12 '15 at 08:01
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);
vladon
  • 8,158
  • 2
  • 47
  • 91