uncertainty in order of execution - any explanation?

This is a discussion on uncertainty in order of execution - any explanation? within the C Programming forums, part of the General Programming Boards category; I have a piece of code which prints the values and addresses of a 2D array: Code: #include<stdio.h> int main(void) ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    uncertainty in order of execution - any explanation?

    I have a piece of code which prints the values and addresses of a 2D array:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
      /* declare array[3][4] which is stored contigurously in memory */
      int array[3][4]={{1,2,3,4},
    	             {5,6,7,8},
                                 {9,10,11,12}};
    
      int *ptr,i,j;
      int nrows=3,ncols=4;
    
      puts("Using array notation...");
      for(i=0; i<nrows; i++)
        for(j=0; j<ncols; j++)
          printf("\narray[%d][%d]=%d",i,j,array[i][j]);
    
      puts("\n\n\nUsing pointer notation...");
      /* type cast since ptr is a pointer to int; array is a 
         constant pointer to an int array[4] */
      ptr=(int *)array;
      for(i=0; i<nrows; i++)
        for(j=0; j<ncols; j++)
          printf("\narray[%d][%d]=%d",i,j,*ptr++);
    
      printf("\n\n");
    
      ptr=(int *)array;
      for(i=0; i<nrows; i++)
        for(j=0; j<ncols; j++)
          printf("\n&array[%d][%d]=%u",i,j,ptr++);
    
    
      printf("\n\n");
    
      /*  ptr=(int *)array;
      for(i=0; i<nrows; i++)
        for(j=0; j<ncols; j++)
        printf("\narray[%d][%d]=%d at address %u",i,j,*ptr++,ptr++);*/
    
      ptr=(int *)array;
      for(i=0; i<nrows; i++)
        for(j=0; j<ncols; j++)
          {
    	printf("\narray[%d][%d]=%d at address %u",i,j,*ptr,ptr);
    	ptr++;
          }
    
      printf("\n\n");
    
      return 0;
    }
    The commented out region did not give me correct result, which
    was obtained after I put the pointer increment out of the printf statement:

    Not working...
    Code:
    printf("\narray[%d][%d]=%d at address %u",i,j,*ptr++,ptr++);*/
    Working...
    Code:
    printf("\narray[%d][%d]=%d at address %u",i,j,*ptr,ptr);
    ptr++;
    However, it was no program when ptr++ or *ptr++ appear singly in a printf statement as in previous examples. I read something about the order of execution of ptr++ and *ptr++ can be uncertain when they are printed together. Any more insightful explanation is much welcome!!! Or any other things similar to this situation that I should look out for?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    1. In your commented out region you have 2 (two) post-inc,but only 1 in the other code.
    2. You change the value of ptr more than once between two sequence points -> undefined behavior.
    3. The format specifer for pointers is p -> undefined behavior.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: uncertainty in order of execution - any explanation?

    Code:
    printf("\narray[%d][%d]=%d at address %u",i,j,*ptr++,ptr++);*/
    When this statement is executed, ptr is incremented after printing the *ptr++ parameter, then incremented again for the ptr++.

    Therefore, the second ptr does not have the value expected.

    Illiustrated with the following code

    Code:
    #include<stdio.h>
    
    int xx[] = {11,22,33,44,55,66,77,88,99,38};
    
    int main()
    {
        int *p,i;
        
        p = xx;
        for (i=0; i<5; i++)
        {
            // display the index, value, address
            printf("%d)  %d %p \n", i, *p, p);
            p++;
        }
        printf("\n");
    
        p = xx;
        for (i=0; i<5; i++)
        {
            // display the index, value, address
            printf("%d)  %d %p \n", i, *p++, p++);
        }
        printf("\n");
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Q3.2 - Q3.8 - Q3.3 - Q3.9
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Wow! Thanks Dave. I never new there were ideosyncracies with ++ and -- like that. I now wonder if the compilers I've used redefined how the auto-inc worked, or I just never used the ++ in a undefined form!

    So bottom line is if a value is inc- or decremented, do not use it elsewhere in the same statement.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by WaltP
    I never new there were ideosyncracies with ++ and -- like that.
    It is not dependent on ++ or --.
    This code snippet outputs three different results on three different compilers (two gcc ports).
    Code:
    int p = 0;
    printf("%d %d\n",p += 1,p += 1);
    changing a value more than once between two sequence points..

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    19
    The order in which arguments to a function are evaluated is not guaranteed. So *never* write code that depends on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 07:52 PM
  2. order of execution of tokens in a c++ source code
    By sandy.sandipanc in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2008, 01:34 PM
  3. Weird code execution order problem
    By Syko in forum C Programming
    Replies: 2
    Last Post: 05-12-2005, 03:29 PM
  4. expression execution order
    By _Elixia_ in forum C Programming
    Replies: 3
    Last Post: 10-02-2003, 05:01 PM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21