Thread: rand() error??

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    rand() error??

    hello all, I have this function which is posted below, and the second time the while loop goes through it crashes. On MSVC++ it terminates the dos window, and on unix I get a floating exeption error.

    I've pin pointed the problem to be where I get the second random number...near the buttom of the function...any ideas??

    axon
    Code:
    void readInFileAndGetChars(string fileName, char text[], int& sum)
    {
    	//declaring variables
    	int i;
    
    	ifstream inStream;
    
    	inStream.open(fileName.c_str()); //open user chosen file
    
    	if ( inStream.fail() ) { //check if file opened right
    		cout << "Input file opening for " << fileName << " failed.  Exiting...\n\n";
    		exit(-1);
    	}
    
    	for ( i=0; i<MAX_SIZE; i++ ){ //input file into the array
    		inStream.get(text[i]);
    	}
    	
    	cout << endl << endl << fileName << " consists of " << MAX_SIZE << " characters.\n\n "
    	     << endl;
    }
    
    //=============================================
    void generateOutput(char text[], int window, int length, int sum)
    {
    	//declaring arrays
    	char *windowArr = new char[window]; //window array
    	int allChars[127];
    
    	//declaring variables
    	int randomPosition = (rand()/2);//( rand()%MAX_SIZE ); //get random number
    	bool notDone = true;
    	int i, j, temp;
    	int count2 = 0; //couter for how many times through loop
    	
    	while(notDone)
    	{
    		int randChar=0;
    		int count = 0;	//counter for comparison part
    		
    		int sum2 = 0;	//sum for all characters that follow window
    		char printChar; //randomly chosen charcter that will follow window
    
    		
    		for( i=0; i<window; i++){ //match random position characters with window
    			if(count2 == 0){	
    				windowArr[i] = text[randomPosition + i];
    			}else{
    				
    				windowArr[i] = windowArr[i+1];
    				windowArr[window-1] = printChar;
    				
    			}
    		}
    
    		for( i=0; i<127; i++){ //set every element to 0
    			allChars[i] = 0;
    		}
    		
    		for( i=0; i<MAX_SIZE; i++){ //COMPARISON
    			count = 1;
    			if( windowArr[0] == text[i] ){
    				for( j=1; j<window; j++ ){
    					if( windowArr[j] == text[i+j] ){
    						count++;
    					}else{ break;}
    					if( count == window ){
    						allChars[ text[j+i+1]]++; //to get next char and add 1 to it
    						break;
    					}else{continue;}
    				}
    			}
    		}
    		
    		for(i=0; i<127; i++){  //get sum of characters which appear at least 1 time
    			if(allChars[i] > 0){
    				sum2 += allChars[i];
    			}
    		}
    		
    		char *lastChar = new char[sum2]; //declare array to hold characters that appear after window
    		temp = sum2;
    		
    		for( i=0; i<127; i++){ //put all characters that appear at least once
    			if( allChars[i] > 0 ){ //into an array
    				while( temp != -1 ){
    					for( j=1; j<=allChars[i]; j++){
    						lastChar[i-i+temp-j] = (char)i;
    					}
    					temp = temp - (1*allChars[i]);
    					break;
    				}			
    			}
    		}
    		/****The PROBLEM IS IN THE LINE BELOW***/
    		randChar = (rand()%sum2); //get random position for next char
    		printChar = lastChar[randChar];
    		
    		cout.flush() << printChar;
    
    		count2++;
    		
    		if( count2 == length ){
    			notDone = false;
    		}
    	}
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You're stomping on memory that don't belong to you.

    I can definately see it in "windowArr[i] = windowArr[i+1]" and "text[randomPosition + i]" and possibly in "text[j+i+1]".

    Go through your code and ensure you aren't going outside the bounds of your arrays.

    You are probably having trouble at the rand() call cuz thats the first call after you've stomped all over everthing.

    gg

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    should I delete the dynamic arrays declared in the while loop at its end?

    if not, how do I solve the problem?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Oh I see what you're syaing now...yes windowArr[i+1] does go over but I don't see how text[ randomPosition + 1] does...text is an array of 500000 elements...anyhow, will this work to fix the windowArr problem?
    Code:
    for( i=0; i<window; i++){ //match random position characters with window
    			if(count2 == 0){	
    				windowArr[i] = text[randomPosition + i];
    			}else{
    				if( i != (window-1)){ 
    					windowArr[i] = windowArr[i+1];
    				}
    				windowArr[window-1] = printChar;
    				
    			}
    		}
    that is what I put in, but I still get the mistake,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Why are you using an array that large? If are trying to ensure against buffer overflows, use the string class.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    that was specified by my assignment! :-(

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    zip up all your code and post it...(and what is it supposed to do!)

    and yes, you have a memory leak if you don't delete anything you new.

    gg

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    first of all...the assignment...in short form is as follows...user pics a file to analize(files provided) than enters a window size, and output length.

    Now, the program opens the specified file, puts the first 500000 characters of that file into an array, randomly picks a position in that array and takes a window size number of characters from it.

    For example...if the array contained {this is the thou those that!} and the window size was 2, and the random position would be 0, the program would put the first "th" into another array.

    Next part is to step through the array containing the 500000 characters to look for any occurences of "th" and taking the character that follows it. Then these characters are sumed up, and put into another array...one of these characters is then randomly hose and printed out...the windo is then moved one to the right, and the whole process repeated again, until the output length of characters is reached.

    THATS just the first part...I hope you understand...by the way I think this is a VERY tough assignment for a first c++ class! anyhow, if you are still unclear here is a link to the assignment.

    in the zipped file I included input.txt because that is what I use for debugging...

    thanks for the help

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    randChar = (rand()%sum2);
    sum2 is 0...you never said you were getting a divide by 0 error.

    gg

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    sh&t! sum2 is equal to zero! I don't know how come...!?!? first time through the loop it is fine. And I'm not getting divide by 0 problem...the program just terminates


    grumble grumble grumble

    axon <--- tired and ........ed off - yet optimistic

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    OK I see what is going on...its not finding another instance so it sets sum2 = 0... to partially fix that I replaced that first loop with
    Code:
    if(count2 == 0){
          for( i=0; i<window; i++){ //match random position characters with window
          windowArr[i] = text[randomPosition + i];
          cout << windowArr[i];
          }
    }else{
    	for( i=0; i<(window-1); i++){ //match random position characters with window
    	windowArr[i] = windowArr[i+1];
    	windowArr[printChar];
    	}
    }
    and if you put window size=2 and 20 output length it works well, but not to the desired effect....any help with the program it self?

    thanks a bunch for your help codeplug!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed