Home > Net >  malloc fails when asking for over 8 GiB
malloc fails when asking for over 8 GiB

Time:10-15

I am trying to load 1024 matrices into an OpenCV Mat. Each matrix is width*height=2200x2200 and each element is float, so it is about 19.36 MB for each matrix. I need to assign 1024 of these matrices which require over 19 GB of memory. This is okay as I have 128GB RAM in my virtual machine.

However, I have problem getting the code to run once I am over 443 matrices and the code produce segmentation fault.

I suspect that the gcc compiler is producing 32bit binary instead of 64bit, but it still failed with -m64 option to g .

Could you have a look at my code and how can I load all these matrices at once?

int frame_num = 1;
int frames = 444;                   // max frames to process 
int hpixels = 2200;                 // number of roi horizontal pixels
int vpixels = 2200;                 // number of roi vertical pixels

unsigned int roi_size = vpixels * hpixels;  

float_t *roi = (float_t *)malloc(frames * vpixels * hpixels * sizeof(float_t));

Mat cv_source(vpixels, hpixels, CV_32FC1, roi   frame_num*roi_size); 

waitKey(0);

free(roi);

return 0; 

CodePudding user response:

The problem is with this expression:

frames * vpixels * hpixels * sizeof(float_t)

As all those variables are of type int, the result seems to overflow.

Try using types size_t for those variables.

Best regards.

  • Related