Problem with big matrices

  • Thread starter Diego Andres Alvarez Marin
  • Start date
D

Diego Andres Alvarez Marin

Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:


#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[j] = (double)i+j;

return EXIT_SUCCESS;
}


In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?

Thanks a lot in advance!

Diego Andres
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Diego said:
Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:


#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[j] = (double)i+j;

return EXIT_SUCCESS;
}


In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.



The array is M*N*sizeof(double) bytes, which is 2000*2000*8 == 32MB.
That isn't much, but a lot if you put it on the stack.
How can I allocate this matrix?

Maybe allocate it from heap or make it static?

Boa
 
P

pete

Diego said:
Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:

#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[j] = (double)i+j;

return EXIT_SUCCESS;
}

In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?



#include <stdlib.h>
#include <stdio.h>

#define M 2001
#define N 2000

int main(void)
{
int i, j;
double (*A)[N];

A = malloc(M * sizeof *A);
if (A == NULL) {
fputs("malloc problem\n", stderr);
return EXIT_FAILURE;
}
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
A[j] = (double)i + j;
}
}
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("%f\n", A[j]);
}
}
free(A);
return 0;
}
 
J

jacob navia

There are probably some limits in the stack size
of the OS you are running on.

Under windows, for instance, stack size is by default 1MB only.

Using lcc-win32 you can increase the stack size at link time.

I compiled your program with
lc tmat.c -stack-reserve 40000000

and it run without any problems.

By the way, your matrix is 2000 * 2000 * 8 == 32MB, not 16.

Maybe there are compiler specific flags to increase the stack size
in your compiler documentation.

jacob
 
S

Stuart Gerchick

Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:


#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[j] = (double)i+j;

return EXIT_SUCCESS;
}


In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?

Thanks a lot in advance!

Diego Andres


First, it is probably 2000*2000*8 for 32 MBS, since they are doubles.
Now 32MB is not that much, however this is going right on the stack
and there are limits set by the compiler to what that max is.

You can
1) Look into compiler options to increase that number
2) Just malloc and put it on the heap
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top