By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

allocating two dimensional array inside for loop

P: 1
HI FRIENDS
i have a problem . i am trying to allocate array inside the for loop .
Below is my code here int** character has been allocated inside the for loop and then i have used the delete operator .

after that for loop will again allocate mamory (different dimension from previous one ).
this is my college project under dead line
thx
Expand|Select|Wrap|Line Numbers
  1. void CDLL3::beginLabel(int** imgs)
  2. {
  3.     int m = 1;
  4.     for(int x=1;x<width-1;x++)
  5.     {
  6.         for(int y=1;y<height-1;y++)
  7.             if(imgs[y][x]==0)
  8.             //    MessageBox(NULL, "entering into comp label", "", 0);
  9.                 compLabel(y,x,++m,imgs);
  10.     }
  11.     int l, k; //Initially comment started from here
  12.     ofstream fout;
  13.     fout.open("output2.txt");
  14.     for (l = 0; l < 80; ++l)
  15.     {
  16.          for (k = 0; k< 290; ++k)
  17.          {
  18.  
  19.             fout << imgs[l][k];
  20.          }
  21.  
  22.                   fout << '\n';
  23.  
  24.     }   
  25.     ////////////////Code For Character Extraction///////////////////
  26.     MessageBox(NULL, "char extraction", "", 0);
  27.     string str="";
  28.     int noOfChar=1;
  29.     for(int i=0;i<height;i++)
  30.     {
  31.         for(int j=0;j<width;j++)
  32.         {
  33.             if(imgs[i][j]>noOfChar)
  34.             {
  35.                             noOfChar = imgs[i][j];
  36.             }
  37.         }
  38.     }
  39.     /////////////////No unend Loop//////////////
  40.     MessageBox(NULL, "before for loop of noOfChar", "", 0);
  41.     //int** character ;
  42.     for(int tmpChar=3;tmpChar<=noOfChar;tmpChar++)
  43.     {
  44.         MessageBox(NULL, "for loop noofchar", "", 0);
  45.          int leftBoundary = width;
  46.          int rightBoundary = 0;
  47.           int lowerBoundary = 0;    
  48.          int upperBoundary = height;
  49.          for(int tmpRow=0;tmpRow<height;tmpRow++)
  50.          {
  51.             for(int tmpCol=0;tmpCol<width;tmpCol++)
  52.             {
  53.               if(imgs[tmpRow][tmpCol]==tmpChar)
  54.               {
  55.                if(tmpCol<leftBoundary)
  56.                {
  57.                    leftBoundary=tmpCol;
  58.                }
  59.                if(tmpCol>rightBoundary)
  60.                {
  61.                    rightBoundary=tmpCol;
  62.                }
  63.                if(tmpRow>lowerBoundary)
  64.                {
  65.                    lowerBoundary=tmpRow;
  66.                }
  67.                if(tmpRow<upperBoundary)
  68.                {
  69.                    upperBoundary=tmpRow;
  70.                }
  71.               }
  72.             }
  73.          }
  74.  
  75.         MessageBox(NULL, "dynamic allocation of array", "", 0);
  76.         int** character = new int*[lowerBoundary-upperBoundary+3] ;
  77.         for(int charRow=0;charRow<(lowerBoundary-upperBoundary+3);charRow++)
  78.         {
  79.             character[charRow]=new int[(rightBoundary-leftBoundary+3)];
  80.         }
  81.         /*
  82.         character = (int**)malloc((lowerBoundary-upperBoundary+3) * sizeof(int *));    
  83.         for(int r = 0; r < (lowerBoundary-upperBoundary+3); r++)
  84.         {
  85.             character[r] = (int*)malloc((rightBoundary-leftBoundary+3) * sizeof(int));
  86.             MessageBox(NULL, "allocating 2d array", "", 0);
  87.         }
  88.         */
  89.         int threshold = 0;
  90.         for(int i=0;i<(lowerBoundary-upperBoundary)+3;i++)
  91.         {
  92.             MessageBox(NULL, "Entering in character", "", 0);
  93.           for(int j=0;j<(rightBoundary-leftBoundary)+3;j++)
  94.           {
  95.              if(imgs[(upperBoundary-1)+i][(leftBoundary-1)+j]==tmpChar)
  96.              {
  97.                 character[i][j]=0;
  98.                 threshold++;
  99.              }
  100.              else
  101.              {
  102.                  character[i][j]=1;
  103.              }
  104.           }
  105.         }
  106.  
  107.         if(threshold>15)
  108.         {
  109.           int optimum = captcha_test(character,(rightBoundary-leftBoundary+3),(lowerBoundary-upperBoundary+3));
  110.           //    delete character;
  111.           <RED>
  112.           for(int row1=0;row1<(lowerBoundary-upperBoundary+3);row1++)
  113.           {
  114.             delete[] character[row1];
  115.             character[row1]=0;
  116.                 MessageBox(NULL, "free character", "", 0);
  117.              // free(character[row1]);
  118.           }
  119.              delete[] character;
  120.           //character= 0;
  121.           //free(character);
  122.           character=0;
  123. <RED>
  124.           if(character==0)
  125.           {
  126.               MessageBox(NULL, "character has been set to null successfully", "", 0);
  127.           }
  128.  
  129.           if(optimum<26)
  130.           {
  131.                     str.append(1,char(10+optimum));
  132.           }
  133.            else
  134.           {
  135.                     str.append(1,char(optimum-24));
  136.           }
  137.  
  138.         }
  139.     }
  140. }
Mar 14 '07 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,646
Hi. Perhaps you should read the posting guidelines particularly with respect to assignments. Also you have not asked a question.
Mar 14 '07 #2

Post your reply

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