How do I create a matrix in C with more than 294 elements, since it falls and gives: RUN FAILED (exit value 1, total time: 469ms) in NeatBeans

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

double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b){
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}

void main() {
int a ;
a=294;
multiplicacionM(a);
}

void multiplicacionM(int n){
long int a[n][n];
long int b[n][n];
long int i=0;
long int j=0;
long int k=0;
long int  temporal;


printf("Llegue 3");
for( i=0;i<n;i++){
    for ( j=0;j<n;j++){
        a[i][j]=rand()%1000;
        b[i][j]=rand()%1000;
    }
}

long int matriz [n][n];


for( i=0;i<n;i++){
    for ( j=0;j<n;j++){
        matriz[i][j]=0;
    }
 }

    //LARGE_INTEGER t_ini, t_fin;
 //double secs;

 //QueryPerformanceCounter(&t_ini);

 for( i=0;i<n;i++){
     for ( j=0;j<n;j++){
                temporal = 0 ;
        for( k=0;k<n;k++){
                        temporal+= a[i][k] * b[k][j];
            matriz[i][j] =temporal;
        }
     }
   }
  //QueryPerformanceCounter(&t_fin);

   //secs = performancecounter_diff(&t_fin, &t_ini);
   //printf("%.16g milisegundos\n", secs * 1000.0);

     printf("Termine");

    }
    
asked by Luis José Castillo 07.04.2017 в 05:50
source

1 answer

2

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.

    
answered by 07.04.2017 в 06:33