NO

Author Topic: Memory problem?  (Read 3006 times)

Ziggurat

  • Guest
Memory problem?
« on: October 02, 2009, 10:14:37 PM »
I've got a program to do some simple calculations which runs into some errors when I try to make an array too large.  The problem is that "too large" isn't very large at all.  I've stripped it down to something which replicates the error but doesn't do much else:

#include <stdio.h>
#define SIZE        301   

int main(int argc, char *argv[])
{
    int i;
   int j;

   double E[SIZE][SIZE];
   FILE *outputfile;

   outputfile = fopen("output.txt", "w");
   for(j = 0; j < SIZE; j++)
   {
      for(i = 0; i < SIZE; i++)
      {
         E[j]=i*j;
         fprintf(outputfile,"%f\t",E[j]);
      }
      fprintf(outputfile,"\n");
   }
   fclose(outputfile);
   return 0;
}

I'm compiling and running this as a simple console program under Windows XP, using Pelles C 6.00.4.  If I make SIZE too big (401 is too big), then the program crashes, and I've got no idea why.  If SIZE is small (301 is small enough), the program runs fine, and I can even do more complicated calculations with the array that I've cut out for the sake of simplicity.  This has the feel of one of those problems which should be easy to solve, if only I knew the key to it, but I don't, and can't make any headway.  Any ideas?  Am I running up against some DOS memory limit or something?  Is there a simple way around it?  Any help would be appreciated.

JohnF

  • Guest
Re: Memory problem?
« Reply #1 on: October 03, 2009, 08:40:52 AM »
I think you'll find that 401*401*8 is too big for the local stack.

Make the array a gobal variable and it should be ok. Or, you can increase the size of the stack.

John

Ziggurat

  • Guest
Re: Memory problem?
« Reply #2 on: October 06, 2009, 01:43:35 AM »
Thanks for your response.  Both solutions seem to work.  Keeping the variable local somehow seems cleaner to me, but is there any downside to making the stack size large?

JohnF

  • Guest
Re: Memory problem?
« Reply #3 on: October 06, 2009, 07:55:40 AM »
Thanks for your response.  Both solutions seem to work.  Keeping the variable local somehow seems cleaner to me, but is there any downside to making the stack size large?

The downside is that if you wanted an even bigger array you would have to increase the stack size yet again.

John