On Friday 14 November 2008 10:09:22 Anna Sidera wrote:
> Hello,
>
> The following code works in lcc in windows but it does not work in gcc in
> unix. I think it is memory problem. In lcc there is an option to use more
> temporary memory than the default. Is there something similar in gcc?
>
> #include <stdio.h>
> #include <math.h>
> #include <stdlib.h>
> #include <time.h>
> int main()
> {
> int i, j;
> int buffer1[250][1000000];
> for (i=0; i<250; i++) {
> for (j=0; j<1000000; j++) {
>          buffer1[i][j]=0;
> }
> }
> printf("\nThe program finished successfully\n");
> return 0;
> }
>
> Many Thanks,
> Anna

Anna,

the code you provided tries to allocate a huge chunk of memory on the stack. 
This is not the way things should be done. Even if the compiler allows 
for "using more temporary memory than the default", the solution is by no 
means portable. A way more elegant solution is to use memory on the heap:

int main()
{
int i, j;
int *buf = (int*) malloc (250 * 1000000 * sizeof(int));
for (i=0; i<250; i++) {
for (j=0; j<1000000; j++) {
buf[i][j]=0;
}
}
free (buf);
printf("\nYay! :D\n");
return 0;
}


Tim

Reply via email to