2d array segmentation fault

This is a discussion on 2d array segmentation fault within the C Programming forums, part of the General Programming Boards category; GOD DAY I MADE A PROGRAM (see below) THAT READS FROM 2 INPUT ARRAYS AND POPULATE A 2D ARRAY. IT ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    2d array segmentation fault

    GOD DAY

    I MADE A PROGRAM (see below) THAT READS FROM 2 INPUT ARRAYS AND POPULATE A 2D ARRAY. IT SEEMS WORKING BUT AT ONE POINT THE PROGRAM STOPS AND SAYS "SEGMENTATION FAULT". MY INPUT ARRAYS ARE LARGE ABOUT 5500X5500. THIS IS ONE OF MY FIRST C PROGRAMS. THANKS FOR YOUR HELP.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NCOL 5524
    #define NROW 5586
    #define ASCOL NCOL*NROW
    #define ASROW 2 
    		
    main (int argc, char **argv) {
    
      FILE *inL1, *inL2;
    
      unsigned char *data_byte1,*data_byte2;
      unsigned char  val1, val2;	
      
      unsigned char** pixassign = NULL;
      int 	f;
      long int i, j, k, q; 
    
         
      /****** Open files and initialize ******/
     if (argc != 3) {
        printf("\n command line: add_value <in file 1> <infile 2> \n");
        exit(1);
      }
        
      if ( (inL1 = fopen(argv[1],"rb")) == NULL ) {
         printf("\n Cannot OPEN INPUT file C1 \n"); 
         exit(1); 
      }
      
      if ( (inL2 = fopen(argv[2],"rb")) == NULL ) {
         printf("\n Cannot OPEN INPUT file C2 \n"); 
         exit(1); 
      }
            
        
      data_byte1 = (unsigned char *)calloc(NCOL, sizeof(unsigned short int));
      if (data_byte1 == NULL) {
    	printf("\n Not enough memory for data short int array \n"); 
        	exit(1);
       }
       data_byte2 = (unsigned char *)calloc(NCOL, sizeof(unsigned short int));
      if (data_byte2 == NULL) {
    	printf("\n Not enough memory for data short int array \n"); 
        	exit(1);
       }
         
      
       /*   defining the OUPUT array */
      
       pixassign = (unsigned char **)calloc(ASCOL*ASROW, sizeof(int));
       if (pixassign == NULL)
           return 0;  // check for malloc fail
    
       for (i = 0; i < ASROW; i++)
       {
           pixassign[i] = (unsigned char *)calloc(ASCOL*ASROW, sizeof(int));
           if (pixassign[i] == NULL)
               return 0;  // check for malloc fail
       }
       
       
    	fread(data_byte1,sizeof(unsigned short int),NCOL,inL1);
    	fread(data_byte2,sizeof(unsigned short int),NCOL,inL2);				
    
    // populate the 2D array
    
      f=0;					
      for (j = 0; j < NROW; j++) { 
    	
        	for (i = 0; i < NCOL; i++) {
    	         					  
    	      		k = i + (j*NCOL);
    			
    			val1 =  data_byte1[k];
    			val2 =  data_byte2[k];
    			
    			 pixassign [0][f] = val1;  
    			pixassign [1][f] = val2;
    		
    		// Test the outputs
    		printf(" column i %d\n", i);
    		printf(" row j %d\n", j);
    		printf("k %d\n", k);
    		printf("Value in data_byte1 : %d\n", val1);
    		printf("Value in data_byte2 : %d\n", val2);
    		printf("f                           : %d\n", f);	 
    			
    			f++;									
    	                   
    	}  /* end for i */
    		        
       } /* end for j */
       	
        	
      /****** Close files ******/
      fclose(inL1);  
      fclose(inL2); 
     
      if (data_byte1 != NULL) free(data_byte1); 
      if (data_byte2 != NULL) free(data_byte2);
     
      for(i = 0; i < ASROW; i++) free(pixassign[i]);
      if (pixassign != NULL) free(pixassign);
      }
    >>> extract of the output>>>>>
    column i 1754
    row j 39
    k 217190
    Value in data_byte1 : 0
    Value in data_byte2 : 0
    f : 217190
    column i 1755
    row j 39
    k 217191
    Value in data_byte1 : 0
    Value in data_byte2 : 0
    f : 217191
    Segmentation fault (core dumped)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Well that's to be expected, if you just pick random types for your sizeof() expressions in your various malloc/calloc calls.

    Typically, you do this, and the problem of guessing the types goes away.
    Code:
    myPointer = malloc( howMany * sizeof(*myPointer) );


    Oh, and one more thing - figure out where your CAPS LOCK key is before posting again.
    Several lines of yelling at us does not go down well.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialising a 3D array - Segmentation fault
    By FuJay in forum C Programming
    Replies: 6
    Last Post: 09-01-2010, 03:05 AM
  2. Segmentation fault while using 2D array
    By Damon Dike in forum C Programming
    Replies: 7
    Last Post: 04-03-2010, 08:23 AM
  3. Segmentation fault (pointer to an array)
    By Lang in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 12:22 AM
  4. Basic array segmentation fault
    By Argentius in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 04:50 AM
  5. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21