allocating two dimensional array inside for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumitmishra
    New Member
    • Mar 2007
    • 1

    allocating two dimensional array inside for loop

    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
    Code:
    void CDLL3::beginLabel(int** imgs)
    {
    	int m = 1;
    	for(int x=1;x<width-1;x++)
    	{
    		for(int y=1;y<height-1;y++)
    			if(imgs[y][x]==0)
    			//	MessageBox(NULL, "entering into comp label", "", 0);
    				compLabel(y,x,++m,imgs);
    	}
        int l, k; //Initially comment started from here
        ofstream fout;
        fout.open("output2.txt");
    	for (l = 0; l < 80; ++l)
    	{
             for (k = 0; k< 290; ++k)
    		 {
    	
    			fout << imgs[l][k];
    		 }
                     
    			      fout << '\n';
    				
    	}   
    	////////////////Code For Character Extraction///////////////////
    	MessageBox(NULL, "char extraction", "", 0);
    	string str="";
    	int noOfChar=1;
    	for(int i=0;i<height;i++)
    	{
    		for(int j=0;j<width;j++)
    		{
    			if(imgs[i][j]>noOfChar)
    			{
    							noOfChar = imgs[i][j];
    			}
    		}
    	}
    	/////////////////No unend Loop//////////////
    	MessageBox(NULL, "before for loop of noOfChar", "", 0);
    	//int** character ;
    	for(int tmpChar=3;tmpChar<=noOfChar;tmpChar++)
    	{
    		MessageBox(NULL, "for loop noofchar", "", 0);
        	 int leftBoundary = width;
    		 int rightBoundary = 0;
    	 	 int lowerBoundary = 0;    
    		 int upperBoundary = height;
    		 for(int tmpRow=0;tmpRow<height;tmpRow++)
    		 {
                for(int tmpCol=0;tmpCol<width;tmpCol++)
    			{
    			  if(imgs[tmpRow][tmpCol]==tmpChar)
    			  {
    			   if(tmpCol<leftBoundary)
    			   {
    				   leftBoundary=tmpCol;
    			   }
    			   if(tmpCol>rightBoundary)
    			   {
    				   rightBoundary=tmpCol;
    			   }
    			   if(tmpRow>lowerBoundary)
    			   {
    				   lowerBoundary=tmpRow;
    			   }
    			   if(tmpRow<upperBoundary)
    			   {
    				   upperBoundary=tmpRow;
    			   }
    			  }
    			}
    		 }
    					 
    		MessageBox(NULL, "dynamic allocation of array", "", 0);
    		int** character = new int*[lowerBoundary-upperBoundary+3] ;
    		for(int charRow=0;charRow<(lowerBoundary-upperBoundary+3);charRow++)
    		{
    			character[charRow]=new int[(rightBoundary-leftBoundary+3)];
    		}
    		/*
    		character = (int**)malloc((lowerBoundary-upperBoundary+3) * sizeof(int *));	
    		for(int r = 0; r < (lowerBoundary-upperBoundary+3); r++)
    		{
    			character[r] = (int*)malloc((rightBoundary-leftBoundary+3) * sizeof(int));
    			MessageBox(NULL, "allocating 2d array", "", 0);
    		}
    		*/
    		int threshold = 0;
    	    for(int i=0;i<(lowerBoundary-upperBoundary)+3;i++)
    		{
    			MessageBox(NULL, "Entering in character", "", 0);
    		  for(int j=0;j<(rightBoundary-leftBoundary)+3;j++)
    		  {
    			 if(imgs[(upperBoundary-1)+i][(leftBoundary-1)+j]==tmpChar)
    			 {
    				character[i][j]=0;
    			    threshold++;
    			 }
    			 else
    			 {
    				 character[i][j]=1;
    			 }
    		  }
    		}
    
    		if(threshold>15)
    		{
    		  int optimum = captcha_test(character,(rightBoundary-leftBoundary+3),(lowerBoundary-upperBoundary+3));
    		  //	delete character;
    		  <RED>
    		  for(int row1=0;row1<(lowerBoundary-upperBoundary+3);row1++)
    		  {
    			delete[] character[row1];
    			character[row1]=0;
    				MessageBox(NULL, "free character", "", 0);
    			 // free(character[row1]);
    		  }
    	   	  delete[] character;
    		  //character= 0;
    		  //free(character);
    		  character=0;
    <RED>
    		  if(character==0)
    		  {
    			  MessageBox(NULL, "character has been set to null successfully", "", 0);
    		  }
    		  
    		  if(optimum<26)
    		  {
    					str.append(1,char(10+optimum));
    		  }
         	  else
    		  {
    					str.append(1,char(optimum-24));
    		  }
    
    		}
    	}
    }
    Last edited by sicarie; Mar 14 '07, 02:08 PM. Reason: Added [code] and [/code] tags
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. Perhaps you should read the posting guidelines particularly with respect to assignments. Also you have not asked a question.

    Comment

    Working...