Thread: #define not working for large values

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    #define not working for large values

    Hi,
    I'm a relative newbie to all this and trying to get a simple image processing program to run for a third year undergrad physics project. The program takes in a text image file which contains a 2D array of ints, performs some process on it (details not that important) and then outputs it to a new text file as a 2D array.

    The code itself works fine for a 128 by 128 array, but when the array size is increased to anything larger than around 300 by 300, the code compiles, and runs but with no output. Putting print statements in the code show that it doesn't get beyond the variable initialisations.

    I want to define an array of around 1000 by 1000 to allow selection of a range of input image sizes. I have tried using enum as an alternative to #define (suggested in a different thread on this site), but this only produced the same result.

    The code shown below is just the skeleton of what is to follow - I will add variability of the image file paths and the processing for the image will come later - for now it simply brightens the image.

    Code:
    #include <stdio.h>
    int main(void)
    {
     enum {NMAX=512, MMAX=512};
     int imagearray[NMAX][MMAX], newimage[NMAX][MMAX];
     int i,j,x,y,bright;
     FILE *fp_in;
     FILE *fout;
     
     fp_in=fopen("TumSrc.txt","r");
     
     for(i=0;i<NMAX;i++)
        for(j=0;j<MMAX;j++) 
        {
           fscanf(fp_in,"%u",&imagearray[i][j]);
           printf("%u \n",imagearray[i][j]);     
        }
     
           printf("Please enter a brightness value: \n", bright);
           scanf("\n%d",&bright);
    
    
        fout=fopen("newimage2.txt","w");
        for (x=0;x<NMAX;x++)
           for (y=0;y<MMAX;y++)
              {if((imagearray[x][y]+bright)<=255)
              newimage[x][y]=imagearray[x][y]+bright;
              else newimage[x][y]=255;
              }
       
         for (x=0;x<NMAX;x++)
           {
            for (y=0;y<MMAX;y++)   
                fprintf(fout,"%u\t",newimage[x][y]);
                fprintf(fout,"\n");
    
           }
     fclose(fp_in);
     fclose(fout);
     return 0;
    }
    I want to allow the user to input image dimensions so that the for loops don't run for the entire array - by defining NMAX and MMAX, the user can specify the for loop sizes using two integer variables (say, n and m). For now I have settled with trying to run for loops up to the values of NMAX and MMAX, but this doesn't work either!!

    Any help or suggestions would be appreciated - I don't want my homework doing for me, just a suggestion as to why it doesn't work for large values would be good.....

    I'm using XP with Borland 5.5 compiler, but have also got same results from windows 2000 with the same compiler.

    Thanks in advance :-)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There is not enough stackspace for such big arrays. Use dynamic array allocation.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Thanks Kurt, that's kind of what I imagined it might be.

    Is there any way I can get around this? I am very reluctant to start learning any c++ code at this stage but I will if I have to. I heard the Heap mentioned in relation to solving these sort of memory problems - is there a way to access this, and if so, how is it done, and will it cause problems in reaccessing the data?

    Thanks again,

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up the malloc() and free() functions in the C standard library.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    shall do - thanks for the help :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM