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); } } }



LinkBack URL
About LinkBacks


