473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reordering of statements and volatile variables

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
7 2243
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DaKoadMunky | last post by:
<CODE> #include <iostream> using namespace std; int Foo(int x,int y) { int result = x; result*=y; result+=y;
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
5
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
16
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.