454,440 Members | 1,411 Online
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

# "bad_alloc at memory location" problem

 P: 6 Hi, we're currently working on a video streaming app. Here is the problem: Expand|Select|Wrap|Line Numbers int ConvertRGBtoYUV(int w, int h, unsigned char *rgb, unsigned int *yuv) {     unsigned int *uu, *vv, *u, *v;       unsigned char R;     unsigned char G;     unsigned char B;       uu = new unsigned int[w*h];       vv = new unsigned int[w*h];       if(uu==NULL || vv==NULL)         return 0;       u = uu;     v = vv;       for (int i = 0; i < w*h; i ++) //rgb = 76032, yuv = 38016     {         R = *rgb;         G = *(rgb+1);         B = *(rgb+2);           //from FOURCC         *(yuv+i) = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16;         *(v+i) =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128;         *(u+i) = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128;           rgb+=3;     }       //do sampling 4:1:1       for(int i=0;i> 2;         *(yuv + w*h + w*h/4 + i)  = ( *(v + i * 4) + *(v + i * 4 + 1 ) + *(v + i * 4 + w ) + *(v + i * 4 + w + 1)) >> 2;     }       delete uu;     delete vv;       return 1; } This function is called for every incoming frame. I assume we are havving trouble with the lines: Expand|Select|Wrap|Line Numbers         uu = new unsigned int[w*h];     vv = new unsigned int[w*h];   that was why i added these: Expand|Select|Wrap|Line Numbers    delete uu;    delete vv;   When the delete lines are not yet there, the app runs normally for just about a minute then it crashes, probably due to lack of memory. But when i add the delete lines, this error occurs: Expand|Select|Wrap|Line Numbers Unhandled exception at 0x7c812a5b in SUPER_PROTOTYPE.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0852fc8c..   we're not really that proficient in c++ so we would like to know if there are other ways to allocate and deallocate memory in c++. So far, we have seen something like "'free' what you 'malloced' and 'delete' what you 'newed'". This is where i got the delete idea, and it's currently not working for us. hope anyone could help, thanks and happy new year ^^ Jan 2 '07 #1