Connecting Tech Pros Worldwide Forums | Help | Site Map

dynamic memory allocation using new

Newbie
 
Join Date: Jan 2007
Posts: 5
#1: Mar 30 '07
hi
i have used the new operator for memory allocation. My code is to display an image in VC++. I am using a header file called as free image to read and write and also save my images. But the problem is once i run my code it gives me an error in runtime. Please can somebody help me out.
The error message : bernsen.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char *image = {"wom1.gif"};
  4. cout<<"image to be loaded";
  5.  
  6. #ifdef FREEIMAGE_LIB
  7.     FreeImage_Initialise();
  8. #endif 
  9.  
  10.     FIBITMAP *dib = GenericLoader(image, 0);
  11.     if(!dib)
  12.         return 0;
  13.  
  14.     cout << "imageloaded" << "\n\n";
  15.  
  16. int wd = FreeImage_GetWidth(dib);
  17. int ht = FreeImage_GetHeight(dib);
  18.  
  19. int i,j;
  20. int max,min,t;
  21.  
  22. double **array;
  23.  
  24. array=new double*[wd];
  25.  
  26. for( i = 0 ; i < ht ; i++ )
  27. {
  28. array[i] = new double[wd];
  29.  
  30. }
  31.  
  32. double **array2;
  33.  
  34. array2=new double*[15];
  35.  
  36. for( i = 0 ; i < 15 ; i++ )
  37. {
  38. array2[i] = new double[15];
  39.  
  40. }
  41.  
  42. for (i=0;i<=15;i++)
  43.            for(j=0;j<=15;j++)
  44.            {
  45.                array2[i][j]=0;
  46.            }
  47.            for (i=0;i<=wd-1;i=1+15)
  48.            {
  49.                for (j=0;j<=ht-1;j=j+15)
  50.                {
  51.                    BYTE *x;
  52.                    for (i=0;i<=15;i++)
  53.                    {
  54.                        x = (BYTE *)FreeImage_GetScanLine(dib, i);
  55.                        for (j=0;j<=15;j++)
  56.                        {
  57.                            array2[i][j]=*x;
  58.                            x++;
  59.                        }
  60.                    }
  61.                    max=array2[0][0];
  62.                    min=array2[0][0];
  63.                    for (i=0;i<15;i++)
  64.                    {
  65.                        for (j=0;j<15;j++)
  66.                        {
  67.                            if (array2[i][j]>max)
  68.                                max=array2[i][j];
  69.                            else 
  70.                                if (array2[i][j]<min)
  71.                                    min=array2[i][j];
  72.                        }
  73.                    }
  74.                    t=(max+min)/2;
  75.                    if((max-min)<15)
  76.                    {
  77.                        for (i=0;i<15;i++)
  78.                        {
  79.                            for (j=0;j<15;j++)
  80.                            {
  81.                                array2[i][j]=0;
  82.                            }
  83.                        }
  84.                    }
  85.                        else
  86.                        {
  87.                            for (i=0;i<15;i++)
  88.                            {
  89.                                for (j=0;j<15;j++)
  90.                                {
  91.                                    if (array2[i][j]>=t)
  92.                                        array2[i][j]=255;
  93.                                    else
  94.                                        array2[i][j]=0;
  95.                                }
  96.                            }
  97.                        }
  98.                    }
  99.            }

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Mar 30 '07

re: dynamic memory allocation using new


an immediate problem
Expand|Select|Wrap|Line Numbers
  1. double **array;
  2.  
  3. array=new double*[wd];
  4.  
  5. for( i = 0 ; i < ht ; i++ )
  6. {
  7. }
the array size is wd yet the for() terminates at ht - sholud the array size be ht or the for() terminate at wd
Newbie
 
Join Date: Jan 2007
Posts: 5
#3: Mar 31 '07

re: dynamic memory allocation using new


Quote:

Originally Posted by horace1

an immediate problem

Expand|Select|Wrap|Line Numbers
  1. double **array;
  2.  
  3. array=new double*[wd];
  4.  
  5. for( i = 0 ; i < ht ; i++ )
  6. {
  7. }
the array size is wd yet the for() terminates at ht - sholud the array size be ht or the for() terminate at wd

Thanks for responding to my problem. Well even if i terminate the for loop with wd the same error occurs. If you see in my code i have used it as a two dimension array so is the declaration of array and array2 correct, if not can you tell me how to declare it.
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#4: Mar 31 '07

re: dynamic memory allocation using new


Quote:

Originally Posted by svbala

Thanks for responding to my problem. Well even if i terminate the for loop with wd the same error occurs. If you see in my code i have used it as a two dimension array so is the declaration of array and array2 correct, if not can you tell me how to declare it.

you define array2 as size 15 * 15 index values 0 to 14
Expand|Select|Wrap|Line Numbers
  1. double **array2;
  2.  
  3. array2=new double*[15];
  4.  
  5. for( i = 0 ; i < 15 ; i++ )
  6. {
  7. array2[i] = new double[15];
  8.  
  9. }
  10.  
however in the following code
Expand|Select|Wrap|Line Numbers
  1. for (i=0;i<=15;i++)
  2.            for(j=0;j<=15;j++)
  3.            {
  4.                array2[i][j]=0;
  5.            }
  6.            for (i=0;i<=wd-1;i=1+15)
  7.            {
  8.  
you index from 0 to 15 - change the i<=15 to i<15
Reply