You are creating 3 matrices, each one of 294 * 294 = 86436
, which gives us a total of 259308
elements; if each element occupies 4 bytes , we have 1037232
bytes (at least) occupied in the stack .
To do what you want, the solution is to use dynamic memory , by calling malloc( )
or calloc( )
to allocate memory , and then calling free( )
to free it :
void multiplicacionM( int n ){
size_t total = n * n; // Total de elementos necesarios.
long int *a = malloc( total * sizeof( long ) );
long int *b = malloc( total * sizeof( long ) );
// Ahora usamos 'calloc( )', para no hacernos monótonos :-)
long int *matriz = calloc( total, sizeof( long ) );
long int i;
long int temporal;
printf( "Llegue 3\n" );
// Inicializamos 'a' y 'b' con valores aleatorios.
for( i = 0; i < total; ++i ) {
a[i] = rand( ) % 1000;
b[i] = rand( ) % 1000;
}
// Inicializamos 'matriz' con 0.
for( i = 0; i < total; ++i )
matriz[i] = 0;
// También podríamos hacer
// memset( matriz, 0, sizeof( long ) * total );
for( i = 0; i < total; ++i ) {
temporal = a[i] * b[i];
matriz[i] = temporal;
}
printf("Terminé\n");
// ¡ Que no se nos olvide liberar la memoria !
free( a );
free( b );
free( matriz );
}
Note that, instead of arrays [X] [Y] , I use a array [X] . Given the calculations you want to do, it's much simpler , and it works because, in C, the memory position of the elements of a matriz[3][3]
[1] [2] [3]
[4] [5] [6]
[7] [8] [9]
is really the same as the memory position of the elements in a matrix [3 * 3]:
[1] [2] [3] [4] [5] [6] [7] [8] [9]
It may not be so clear, but it works exactly the same in both ways.