By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

reordering of statements and volatile variables

P: n/a
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 ...

Jun 12 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jun 12, 8:24 am, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
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.
Jun 12 '07 #2

P: n/a
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 ...
Jun 13 '07 #3

P: n/a
anson <kz****@gmail.comwrites:
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/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 13 '07 #4

P: n/a
On Jun 14, 4:08 am, Keith Thompson <k...@mib.orgwrote:
anson <kzj...@gmail.comwrites:
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/>.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
CPU just a joke...~~
Jun 14 '07 #5

P: n/a
On Jun 12, 12:24 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
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
---
Regards,
Taran

Jun 14 '07 #6

P: n/a
Taran wrote:
On Jun 12, 12:24 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
>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.
No, it can't remove the assignments, the variables in question are volatile.

--
Ian Collins.
Jun 14 '07 #7

P: n/a
Taran said:
On Jun 12, 12:24 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
>>
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 */
<snip>
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.