Thread: Large arrays, malloc, and strcmp

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

    Large arrays, malloc, and strcmp

    I have two fairly large 2d arrays to hold strings, and I want to compare each string in the second array to every string in the first to see if any match. The following is how I create the arrays, and how I compare them. It works if x(which is unsigned long b/c max number can be 4000000000) is a small number, but when it is a large number( 500000+ ), the programs seg faults at strcmp. Any ideas?

    Code:
    unsigned long int x;
    int y;
    
    printf( "Enter the number of random words to be produced: " );
    	scanf( "%u", &x );
    
    printf( "Enter the maximum length of each word: " );
    	scanf( "%d", &y);
    
    char **buffer;
    	buffer = malloc( x * sizeof(char *) + 1);
    	for( i=0; i<x; i++) {
    		buffer[i] = malloc( y * sizeof(char) + 1 );
    	}
    
    // Generate random words and store in a 2d buffer array.
    // rand()%25 ensures only letters are generated.
        
    	for( i=0; i<x; i++ ) {
    		for( j=0; j<y-1; j++ ) {
    			
    			r = rand()%25;
    			buffer[i][j] = letters[r];
    			
    		}
    		// Ensure each string is null-terminated.
    		buffer[i][j] = '\0';
    	}
    
    FILE *dict;
    		dict = fopen( "linux.words", "r" );
    		
    	if( ! dict ) {
    		printf( "Error: Failed to open linux.words\n" );
    	}
    	
    		
    	char dict_words[SIZE][WORD_SIZE], *z;
    	i = 0;
    	while( fgets( dict_words[i], 256, dict ) != NULL ) {
    		
    		i++;
    	}
    	
    	// Remove newline character from each word to avoid comparison errors later.
    	int p, len;
    	for( p=0; p<i; p++ ) {
    		if(( z = strchr( dict_words[p], '\n' ) )) *z = 0;
    	}
    	
    	// Ensures all words in linux.words start with lower case letter.
    	for( p=0; p<i; p++ ) {
    		dict_words[p][0] = tolower( dict_words[p][0] );
    	}
    
    
    	}
    	int k;
    	for( j=0; j<i; j++ ) {
    		for( k=0; k<x; k++ ) {
    			if( strcmp( dict_words[j], buffer[k] ) == 0 ) {
    				fputs( buffer[k], words_out);
    				fputs( "\n", words_out);
    				
    			}
    		}
    	}

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're not checking the return value of your malloc()'s for NULL, plus you didn't say how big y is (the total amount of memory you're allocating is roughly x*y*sizeof(char *)).

    Edit: Never mind my second comment, I overlooked the part about strcmp().

    Edit: Your code is not self-contained - for example, the values of SIZE and WORD_SIZE, whatever they are, must be large enough. You also haven't given the definition of letters[].
    Last edited by robatino; 09-24-2007 at 09:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Problem with strcmp()....
    By LightsOut06 in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 12:30 PM
  4. Using malloc() & qsort with a union revisited
    By Cyber Kitten in forum C Programming
    Replies: 11
    Last Post: 11-24-2001, 06:07 PM