By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
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
  1. int ConvertRGBtoYUV(int w, int h, unsigned char *rgb, unsigned int *yuv)
  2. {
  3.     unsigned int *uu, *vv, *u, *v;
  4.  
  5.     unsigned char R;
  6.     unsigned char G;
  7.     unsigned char B;
  8.  
  9.     uu = new unsigned int[w*h];
  10.       vv = new unsigned int[w*h];
  11.  
  12.     if(uu==NULL || vv==NULL)
  13.         return 0;
  14.  
  15.     u = uu;
  16.     v = vv;
  17.  
  18.     for (int i = 0; i < w*h; i ++) //rgb = 76032, yuv = 38016
  19.     {
  20.         R = *rgb;
  21.         G = *(rgb+1);
  22.         B = *(rgb+2);
  23.  
  24.         //from FOURCC
  25.         *(yuv+i) = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16;
  26.         *(v+i) =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128;
  27.         *(u+i) = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128;
  28.  
  29.         rgb+=3;
  30.     }
  31.  
  32.     //do sampling 4:1:1
  33.  
  34.     for(int i=0;i<w*h/4;i++)
  35.     {
  36.         *(yuv + w*h + i)      = ( *(u + i * 4) + *(u + i * 4 + 1)  + *(u + i * 4 + w)  + *(u + i * 4 + w + 1)) >> 2;
  37.         *(yuv + w*h + w*h/4 + i)  = ( *(v + i * 4) + *(v + i * 4 + 1 ) + *(v + i * 4 + w ) + *(v + i * 4 + w + 1)) >> 2;
  38.     }
  39.  
  40.     delete uu;
  41.     delete vv;
  42.  
  43.     return 1;
  44. }
This function is called for every incoming frame. I assume we are havving trouble with the lines:

Expand|Select|Wrap|Line Numbers
  1.         uu = new unsigned int[w*h];
  2.     vv = new unsigned int[w*h];
  3.  
that was why i added these:

Expand|Select|Wrap|Line Numbers
  1.    delete uu;
  2.    delete vv;
  3.  
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
  1. Unhandled exception at 0x7c812a5b in SUPER_PROTOTYPE.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0852fc8c..
  2.  
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
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
Instead of saying

Expand|Select|Wrap|Line Numbers
  1. delete uu;
  2. delete vv;
as you have, use

Expand|Select|Wrap|Line Numbers
  1. delete [] uu;
  2. delete [] vv;
Using the brackets between delete and the variable tells the processor to deallocate the entire array of values. Without the brackets, you are merely deallocationg the first element of uu and vv.
Jan 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.