Thread: Going beyond bounds problem

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Going beyond bounds problem

    Hey guys

    Happy new year first off!

    I am creating the tortoise/hare sim but seem to be running into a bit of a problem. The code compiles fine, and the program runs for the 30 seconds no problem.

    The issue comes at the end I get a "stack around 'racetrack' was corrupted' seg fault which generally means I have gone out of the bounds of the one dimensioanal array.

    The thing is, I did my best to prevent this happening by passing a single addtional varialbe to each movement function to test if the new value was
    below zero or over the array's allocated size - then it would return nothing and quit the function without changing the position of the character.

    Here was what I mean:

    Code:
    // function to move the turtle three squares to the right
    std::string turtFastPlod ( char rctrk[], const char *tur, int &rtl ) {
    	std::string str = "\n\nTURTLE: FAST PLOD\n";
    
    	rtl += 3; // add the value to reference
                    
                    // check if beyond array bounds - if so quit action
    	if (( rtl < 0 ) || ( rtl > 69 )) {
    		rtl = 0;
    		return "";
    	}
    
    	rctrk[ +3 ] = *tur;
    
    	return str;
    }
    And this is part of the main game functon that calls this:

    Code:
    int playGame ( char rctrk[], const char *tur, const char *hre ) {
    	// these are used to check if the T or H go beyond array
    	// boundarys
    	int turtleLimit = 0,
    		hareLimit = 0;
    
    	for ( int i = 0; i <= 30; ) {
    		for ( int j = 1; j <= 3; j++ ) {
    			std::cout << "\n";
    		}
    
    		int eventChoice =  ( 1 + rand() % 8 );
    
    		std::cout << std::setw ( 52 ) << announceStart() << "\n\n";
    
    		std::cout << "\nSeconds Lapsed: " << i << "\n\n";
    
    		printRaceCourse ( rctrk );
    
    		switch ( eventChoice ) {
    			case 1:
    				std::cout << turtFastPlod ( rctrk, tur, turtleLimit );
    				break;
    The race track array passed from main is

    Code:
    const int ARRAY_SIZE = 70;
    
    	char raceTrack[ ARRAY_SIZE ] = { 0 };
    I must be somthing simple I am doing wrong - I have trying to suss it for a
    while and cannot understand what I am doing wrong. Any advice greatly
    appriciated.
    Double Helix STL

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What do the other 7 cases in your switch do?

    > rctrk[ +3 ] = *tur;
    Not
    rctrk[ rtl ] = *tur;
    ?

    > if (( rtl < 0 ) || ( rtl > 69 ))
    Use the ARRAY_SIZE constant
    Are other functions using a different hard-coded constant?
    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
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks Salem.

    I using the constant got rid of the seg fault.

    But the problem is now the board is not being updated on each tick count of the timer.

    Il post the whole game function:

    Code:
    int playGame ( char rctrk[], const char *tur, const char *hre, const int SIZE ) {
    	// these are used to check if the T or H go beyond array
    	// boundarys
    	int turtleLimit = 0,
    		hareLimit = 0;
    
    	for ( int i = 0; i <= 30; ) {
    		for ( int j = 1; j <= 3; j++ ) {
    			std::cout << "\n";
    		}
    
    		int eventChoice =  ( 1 + rand() % 8 );
    
    		std::cout << std::setw ( 52 ) << announceStart() << "\n\n";
    
    		std::cout << "\nSeconds Lapsed: " << i << "\n\n";
    
    		printRaceCourse ( rctrk, SIZE );
    
    		switch ( eventChoice ) {
    			case 1:
    				std::cout << turtFastPlod ( rctrk, tur, turtleLimit, SIZE );
    				break;
    
    			case 2:
    				std::cout << turtSlip ( rctrk, tur, turtleLimit, SIZE );
    				break;
    
    			case 3:
    				std::cout << turtSlowPlod ( rctrk, tur, turtleLimit, SIZE );
    				break;
    
    			case 4:
    				std::cout << hareSleep ( rctrk, hre );
    				break;
    
    			case 5:
    				std::cout << hareBigHop ( rctrk, hre, hareLimit, SIZE );
    				break;
    
    			case 6:
    				std::cout << hareBigSlip ( rctrk, hre, hareLimit, SIZE );
    				break;
    
    			case 7:
    				std::cout << hareSmallHop ( rctrk, hre, hareLimit, SIZE );
    				break;
    
    			case 8:
    				std::cout << hareSmallSlip ( rctrk, hre, hareLimit, SIZE );
    				break;
    
    			default:
    				std::cout << "\n\nERROR!\n\n";
    				break;
    		}
    
    		Sleep(1000);
    		clear_screen();
    		i++;
    	}
    
    	return 0;
    }
    And the change to the movement functions ( all right look the same as this bar a different
    string being returned for the movement text

    Code:
    std::string turtFastPlod ( char rctrk[], const char *tur, int &rtl, const int SIZE ) {
    	std::string str = "\n\nTURTLE: FAST PLOD\n";
    
    	rtl += 3;
    
    	if (( rtl < SIZE ) || ( rtl > SIZE )) {
    		return "";
    	}
    
    	rctrk[ rtl ] = *tur;
    
    	return str;
    }
    It has got somthing to do with the value I am giving rctrk[ ] but I am not sure what I am doing
    wrong. Did I make the mistake removing the zero the rtl?
    Double Helix STL

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (( rtl < SIZE ) || ( rtl > SIZE ))
    Mmm, rtl == SIZE is the only thing which gets past this test, and that writes past the end of the array.

    Use a debugger, and start single-stepping.
    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.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    if (rtl <= 0)
    {
      rtl += SIZE;
    }
    
    if (rtl > SIZE)
    {
       rtl -= SIZE;
    }
    This will allow your indices to wrap around. This method is inclusive of zero but exclusive of SIZE.
    If you simply want to check for bounds then your <= comparison should be against 0 not SIZE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM