reordering of statements and volatile variables

J

junky_fellow

Hi,

Consider the following piece of code:

int main(void)
{
volatile int i;
volatile int k;

i=100; /* line 1 */
k=200; /* line 2 */
i=300; /* line 3 */
k=400; /* line 4 */

return 0;
}

I want to know if the compiler can reorder any of the lines 1 to 4 ?
I believe that, the line 3 will always be executed after line 1, as
'i' is volatile and compiler will not do any optimization on 'i".

Similary, line 4 will always be executed after line 2.

However, line 2 may be executed before line 1 and line 3 may be
executed before line 2.
Similarly, line 4 may be executed before line 3.

Please let me know if my assumption is correct or not ?

thanks a lot for any help ...
 
C

christian.bau

Hi,

Consider the following piece of code:

int main(void)
{
volatile int i;
volatile int k;

i=100; /* line 1 */
k=200; /* line 2 */
i=300; /* line 3 */
k=400; /* line 4 */

return 0;

}

I want to know if the compiler can reorder any of the lines 1 to 4 ?
I believe that, the line 3 will always be executed after line 1, as
'i' is volatile and compiler will not do any optimization on 'i".

Similary, line 4 will always be executed after line 2.

However, line 2 may be executed before line 1 and line 3 may be
executed before line 2.
Similarly, line 4 may be executed before line 3.

Please let me know if my assumption is correct or not ?

thanks a lot for any help ...

All four assignments must happen, and they must happen exactly in the
specified order.
 
A

anson

even the result you can see is ordered .
but i think CUP will execute them out of order ,..
and then CUP know that's OK, according some way .

compiler cannot confirm the order of execute.
CUP do that ...
 
K

Keith Thompson

anson said:
even the result you can see is ordered .
but i think CUP will execute them out of order ,..
and then CUP know that's OK, according some way .

compiler cannot confirm the order of execute.
CUP do that ...

What??

Do you mean "CPU" rather than "CUP"?

Please provide context when you post a followup.
See <http://cfaj.freeshell.org/google/>.
 
T

Taran

Hi,

Consider the following piece of code:

int main(void)
{
volatile int i;
volatile int k;

i=100; /* line 1 */
k=200; /* line 2 */
i=300; /* line 3 */
k=400; /* line 4 */

return 0;

}

I want to know if the compiler can reorder any of the lines 1 to 4 ?
I believe that, the line 3 will always be executed after line 1, as
'i' is volatile and compiler will not do any optimization on 'i".

Similary, line 4 will always be executed after line 2.

However, line 2 may be executed before line 1 and line 3 may be
executed before line 2.
Similarly, line 4 may be executed before line 3.

Please let me know if my assumption is correct or not ?

thanks a lot for any help ...


Well if the compiler is smart then it may recognize that i=300;
does'nt have any affect as it is soon updated with another value
without the older being consumed (same goes for k) and remove the two
instructions completely. Then in that case you would have only
instructions executing i=300; and k=400;. You line 1 and line 2
completely removed.

The compiler may also resequence the instructions of independent parts
of the same code. Windrvier Diab does that and just in case if you do
an Object Code verification, don't get startled if you find and
instruction which doesn't map to next few high level code. It will map
to some code few more lines down!

HTH
 
I

Ian Collins

Taran said:
Well if the compiler is smart then it may recognize that i=300;
does'nt have any affect as it is soon updated with another value
without the older being consumed (same goes for k) and remove the two
instructions completely. Then in that case you would have only
instructions executing i=300; and k=400;. You line 1 and line 2
completely removed.
No, it can't remove the assignments, the variables in question are volatile.
 
R

Richard Heathfield

Taran said:

Well if the compiler is smart then it may recognize that i=300;
does'nt have any affect as it is soon updated with another value
without the older being consumed (same goes for k) and remove the two
instructions completely.

I suggest you look up "volatile" in your C book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top