Is the array value checked as a condition to stop the loop?

Example:

for (int i = 0; i < a[index]; i++) {
      // do stuff
}

      

Will he a[index]

read it every time? If not, what if someone wants to change the value in a[index]

the loop? I've never seen it myself, but does the compiler make such an assumption?

If the condition was instead i < val-2

, will it be evaluated every time?

+3


source to share


3 answers


The compiler usually performs optimizations when the system is not affected by other parts of the program. Therefore, if you make changes inside the loop for

to the condition parameter, the compiler will not optimize.

As mentioned, the compiler must read the array and check it before each iteration in the code snippet. You can optimize your code like this, then it will only read the array once to check the status of the loop.



int cond = a[index];
for (int i = 0; i < cond; i++) {
      // do stuff
}

      

+3


source


well, maybe.

A standards-compliant compiler will generate code that behaves as if read every time.



If index

and / or array

have a storage class volatile

, they will be re-read each time.

If they are not, and the contents of the loops do not use them in such a way that their value can be changed, the optimizer may decide to use the cached result instead.

+2


source


Co does not store the results of expressions in temporary variables. This way, all expressions are evaluated in-place. Note that any loop for

can be changed to a loop while

:

for ( def_or_expr1 ; expr2 ; expr3 ) {
    ...
}

      

becomes:

def_or_expr1;
while ( expr2 ) {
    ...
cont:
    expr3;
}

      

Update: The continue

loop for

will be the same as the goto cont;

int while loop above. That is, it is expr3

evaluated for each iteration.

The compiler can basely apply any optimization to prove that it will not change the essence of the program. It's too far to go into details, but overall it can (and will) optimize:

  • a[index]

    does not change in the loop: read once before the loop and keep pace (e.g. in register).
  • a[index]

    changes in a loop: updates temp (register) with a new value, avoiding memory access (and index computation).

To do this, the compiler must assume that the array does not change outside of visible control flow. This is usually a compiled file (with all files included). For modern systems using connection time optimization (LTO), this can be the whole final program - minus the dynamic libraries.

Please note that this is a very short description. In fact, the C standard defines a pretty clear idea of ​​how a program should run and what / how the compiler can optimize.

If the array is changed, for example, by an interrupt handler or another thread, things get complicated. Depending on your purpose, you will need from volatile

, atomic operations ( stdatomic.h

starting in C11) up to thread / mutex / semaphore / etc locks. to control access to a shared resource.

+1


source







All Articles