Thread: (Core dumped) ¿A too large array?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    26

    (Core dumped) ¿A too large array?

    I'm writing a code which implementes the cell-to-cell mapping method.

    Finally I know why my compiler is saying me there is a segmentation fault. Apparently it's because I defined a matrix of DIMENSION^2xDIMENSION^2, where DIMENSION is 100.

    I need that matrix; i'm declaring the matrix as
    Code:
     int prob[DIMENSION*DIMENSION][DIMENSION*DIMENSION]
    ¿There is a way to declare it in a way there is no error?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Think about what the size is - it's 400MB

    You could replace 'int' with 'char', but perhaps you need to explore ways of partitioning your problem into smaller data sets.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's probably OK if you make it a global/static array, but not if you have it as an automatic local variable - it uses 10000 x 10000 = 100000000 cells *4 bytes, which is far too much for stack storage.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    ok. I don't see how to write in another way my code. Here is what I'm triyng to do.

    I divided and x-y plane in little cell, each of 0.04*0.04. In each cell I started an initial condition of a dynamical system (Duffing-van Der Pol oscilator), and I solve it with RK4. In each cell I chose a number of random intial conditions; clearlñy each cell is characterized by two numbers, i and j. So, I use n to describe such a number.

    Code:
    for(i=0;i<DIMENSION;i++){
      for(j=0;j<DIMENSION;j++){
       n=i*200+j;
       l=j*200+i;
    
       
      prob[n][l]=0;
       
       for(k=0;k<RANDOM;k++){
          
    	xout[0]= (drand48()+i-DIMENSION/2)/25.0;
    	xout[1]= (drand48()+j-DIMENSION/2)/25.0;
    
    	
    
    	for(m = 0; m< NSTEPS; m++){
        	  for(u=0;u<NDIM;u++) xin[u]=xout[u];
    	  runge_kutta (xin, tau, param, xout);
    	         }
    		salidax[n][k]=xout[0];
    		saliday[n][k]=xout[1];		
               }
    	}
    }
    Then I verified in wich cell the output is. Finally, if cell "n" leds to cell "l", probability of n->l increses by one.

    Code:
       for(n=0;n<DIMENSION*DIMENSION;n++){
    	   for(i=0;i<DIMENSION;i++){
      		for(j=0;j<DIMENSION;j++){
    		l=i*200+j;
    		
    		for(k=0;k<RANDOM;k++){
    
    	         if((i-DIMENSION/2)/25.0<salidax[n][k]<(i+1-DIMENSION/2)/25.0)
    		   if((j-DIMENSION/2)/25.0<saliday[n][k]<(j+1-DIMENSION/2)/25.0) prob[n][l]++;
                      }
               
    	    }
              }
            }
    And that's it. But I'm not clever enogh to see another solution. ¿Do you have sugestions?

    Thanks for your answers!

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    &#191;There is a way to define the array dynamically? &#191;Can I declare it like
    Code:
    int *prob
    , without giving the size it will have?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sure, if the minor size is constant, then it's one easy declaration and malloc to allocate the whole array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DIMENSION 100
    
    int main() {
        int (*prob)[DIMENSION*DIMENSION];
        printf("Allocating &#37;ld\n", DIMENSION*DIMENSION * sizeof *prob );
        prob = malloc( DIMENSION*DIMENSION * sizeof *prob );
    
        prob[0][0] = 0;
    
        free( prob );
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    26

    Thumbs up

    I'm trying with this, and at the moment it seems to work fine.

    Code:
      
    int **prob=(int **)malloc(DIMENSION*DIMENSION*sizeof (int*));
    
      for(i=0;i<DIMENSION*DIMENSION;i++){
        prob[i]=(int*)malloc(DIMENSION*DIMENSION*sizeof(int));
      if (prob == NULL) {
        printf("No hay ubicación de memoria suficiente");
        exit(0);
                  }
          }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you read the FAQ yet on why casting malloc is bad ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    Sorry, no.

    I changed my code:


    Code:
     int **prob;
      prob=malloc (DIMENSION * DIMENSION*sizeof ( int* ));
    
       if ( prob == NULL ) {
        printf("No hay ubicaci&#243;n de memoria suficiente");
        exit(0);
       }
    
    
      for (i=0; i < DIMENSION * DIMENSION; i++ ){
    
        prob[i]=malloc(DIMENSION * DIMENSION * sizeof(int));
    
      if (prob[i] == NULL) {
        printf("No hay ubicaci&#243;n de memoria suficiente");
        exit(0);
    
      }
      }
    Thanks for the advise... I didn't know about casting.
    Last edited by Isolda_; 09-11-2007 at 02:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 12:23 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM