Thread: Awesome C Syntax

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Awesome C Syntax

    If you look at this code, everythink works just fine.

    Code:
    int filter_all_legal_moves(BOARD_STATE* pboard_state, MOVE_DATA* pmove_data){	//check all moves
    	for(int i = 0; i < pmove_data->used_array_size; i++){
    		BOARD_STATE bstate = make_move_with_copy(pboard_state, pmove_data->moves[i]);
    		MOVE_DATA new_move_data = return_all_moves(&bstate, 0);
    		if(can_king_be_killed(&bstate, &new_move_data)){
    			pmove_data->moves[i] = "\0\0";
    		}
    	}
    	int i = -1;
    	int p = -1;
    	while(1){
    		i++;
    		do ++p; while(p < pmove_data->used_array_size && pmove_data->moves[p] == "\0\0");
    		if (p >= pmove_data->array_size) break;
    		pmove_data->moves[i] = pmove_data->moves[p];
    
    
    	}
    	pmove_data->used_array_size = i;
    	//check e.p
    	//check castling
    	return 1;
    }
    Now let us write the code like this:

    Code:
    int filter_all_legal_moves(BOARD_STATE* pboard_state, MOVE_DATA* pmove_data){	//check all moves
    	for(int i = 0; i < pmove_data->used_array_size; i++){
    		BOARD_STATE bstate = make_move_with_copy(pboard_state, pmove_data->moves[i]);
    		MOVE_DATA new_move_data = return_all_moves(&bstate, 0);
    		if(can_king_be_killed(&bstate, &new_move_data)){
    			pmove_data->moves[i] = "\0\0";
    		}
    	}
    	int i, p = -1;
    	while(1){
    		i++;
    		do ++p; while(p < pmove_data->used_array_size && pmove_data->moves[p] == "\0\0");
    		if (p >= pmove_data->array_size) break;
    		pmove_data->moves[i] = pmove_data->moves[p];
    
    
    	}
    	pmove_data->used_array_size = i;
    	//check e.p
    	//check castling
    	return 1;
    }
    The debugger tells me i = 7 after initalization...
    Why?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the second example, you declared i but did not initialise it, so it was given a garbage initial value. You probably wanted to write:
    Code:
    int i = -1, p = -1;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > pmove_data->moves[i] = "\0\0"
    ...
    > pmove_data->moves[p] == "\0\0"
    This isn't guaranteed to work, and it isn't how you compare strings.

    It is common for compilers to combine identical string constants, such that say this
    Code:
    if ( "hello" == "hello" ) {
      // string constants merged
    } else {
      // string constants not merged
    }
    will usually result in true rather than false.

    You need a singular char pointer which is otherwise unused elsewhere in your program, eg.
    Code:
    char dummy[];
    
    //....
    int filter_all_legal_moves(BOARD_STATE* pboard_state, MOVE_DATA* pmove_data){   //check all moves
        for(int i = 0; i < pmove_data->used_array_size; i++){
            BOARD_STATE bstate = make_move_with_copy(pboard_state, pmove_data->moves[i]);
            MOVE_DATA new_move_data = return_all_moves(&bstate, 0);
            if(can_king_be_killed(&bstate, &new_move_data)){
                pmove_data->moves[i] = dummy;
            }
        }
        int i = -1;
        int p = -1;
        while(1){
            i++;
            do ++p; while(p < pmove_data->used_array_size && pmove_data->moves[p] == dummy);
            if (p >= pmove_data->array_size) break;
            pmove_data->moves[i] = pmove_data->moves[p];
     
     
        }
        pmove_data->used_array_size = i;
        //check e.p
        //check castling
        return 1;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Awesome?
    By pianorain in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-01-2005, 05:08 PM
  2. this is awesome
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-27-2004, 03:45 PM

Tags for this Thread