Thread: Loop and Side-Effect

  1. #16
    Banned
    Join Date
    Jul 2022
    Posts
    112
    2. Handle error block in while loop,
    - Set variable "err" to TRUE;
    - Exit while loop;

    Handle error block outside while loop;
    - Clean up

    3. Array[i] is null before while loop,
    therefore it will never be executed.

    Note that array[i] changes in while loop.



    Also don't deduce anything from a dummy code..
    The boolean method is good, much better than an infinite loop.
    Last edited by kodax; 11-02-2022 at 08:45 PM.

  2. #17
    Banned
    Join Date
    Jul 2022
    Posts
    112
    80. Loops should not have more than one "break" or "goto" statement
    Coding Rules

    MISRA C:2004, 14.6 - For any iteration statement there shall be at most one break statement used for loop termination.
    MISRA C++:2008, 6-6-4 - For any iteration statement there shall be no more than one break or goto statement used for loop termination.
    MISRA C:2012, 15.4 - There should be no more than one break or goto statement used to terminate any iteration statement.

  3. #18
    Banned
    Join Date
    Jul 2022
    Posts
    112
    These rigid rules points to the fact that Misra is pretty much against infinite loops.

  4. #19
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by kodax View Post
    2. Handle error block in while loop,
    - Set variable "err" to TRUE;
    - Exit while loop;

    Handle error block outside while loop;
    - Clean up

    3. Array[i] is null before while loop,
    therefore it will never be executed.

    Note that array[i] changes in while loop.



    Also don't deduce anything from a dummy code..
    The boolean method is good, much better than an infinite loop.
    Judging by how small the function is I question the logic in handling errors outside the loop, just handle it inside and return:
    Code:
    int  catch_error(char *pos);
    
    int foo(char array[]) {
    	int i = 0, used = 0;
    	if ( !array )
    		return -1;
    	while ( array[i] )
    	{
    		used = 1;
    		if (catch_error(array + i))
    		{
    			// Whatever error handling you need here
    			return -1;
    		}
    		// more code
    		i += used;
    	}
    	return i;
    }

  5. #20
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by kodax View Post
    These rigid rules points to the fact that Misra is pretty much against infinite loops.
    I read no such thing in those rules you quoted.

    To circle back to the code in the original post...


    Those MISRA rules are saying that this is acceptable:

    Code:
    while (array[i]) {
     
        // more code
     
        if (abc()) {
            break;
        }
        i = 1 + i;
    }
    Isn't that fully compliant with "MISRA C:2012, 15.4 - There should be no more than one break or goto statement used to terminate any iteration statement."

  6. #21
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Judging by how small the function is I question
    One can only wonder the powers that you possess,
    to judge something that you can't see, and to question it.

    Powers that the common man can only dream of, no doubt.

  7. #22
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by kodax View Post
    One can only wonder the powers that you possess,
    to judge something that you can't see, and to question it.

    Powers that the common man can only dream of, no doubt.
    Amazing powers, like posting 8 lines of code that can't be run because it is incomplete, asking for opinions:

    Code:
    char empty[1] = {0};
    while (array[i]) {
      if (abc()) {
        array = empty;
        i = -1;
      }
      i = 1 + i;
    }
    then thinking it is ok to get snarky with people for making assumptions:


    One can only wonder the powers that you possess,
    to judge something that you can't see, and to question it.
    That is almost literally what you have asked everybody here to do.

    Also you:


    Also don't deduce anything from a dummy code..

    Well, perhaps in future post the actual code that shows the actual problem, not some incomplete snippet.

    Maybe next time post something complete, that will actually compile and demonstrate the issue????

  8. #23
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by kodax View Post
    One can only wonder the powers that you possess,
    to judge something that you can't see, and to question it.

    Powers that the common man can only dream of, no doubt.
    Quote Originally Posted by kodax View Post
    Can this be considered as a side-effect, bad practice ?

    Code:
    char empty[1] = {0};
    
    while (array[i]) {
    
        // more code
    
        if (abc()) {
            array = empty;
            i = -1;
    
        }
        i = 1 + i;
    }
    Need I say more?

  9. #24
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by hamster_nz View Post
    Amazing powers, like posting 8 lines of code that can't be run because it is incomplete, asking for opinions:

    Code:
    char empty[1] = {0};
    while (array[i]) {
      if (abc()) {
        array = empty;
        i = -1;
      }
      i = 1 + i;
    }
    then thinking it is ok to get snarky with people for making assumptions:




    That is almost literally what you have asked everybody here to do.

    Also you:





    Well, perhaps in future post the actual code that shows the actual problem, not some incomplete snippet.

    Maybe next time post something complete, that will actually compile and demonstrate the issue????
    I really should read other posts more often, ended up doubling up on your reply

  10. #25
    Banned
    Join Date
    Jul 2022
    Posts
    112
    This is a dummy code, what a lack common sense !!

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another kodax bait-and-switch strawman nonsense garbage thread bites the dust.
    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. Replies: 4
    Last Post: 09-14-2017, 11:44 AM
  2. Replies: 7
    Last Post: 10-14-2016, 12:02 PM
  3. Replies: 26
    Last Post: 03-08-2011, 05:23 PM
  4. Replies: 2
    Last Post: 11-13-2009, 05:33 AM
  5. Side effect of various operations
    By xds4lx in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 10:12 AM

Tags for this Thread