equal
deleted
inserted
replaced
|
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html> |
|
2 <head> |
|
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> |
|
4 <meta http-equiv="Content-Style-Type" content="text/css" /> |
|
5 <meta name="LASTUPDATED" content="06/17/05 11:09:43" /> |
|
6 <title>Volatile Variables</title> |
|
7 <link rel="StyleSheet" href="../../book.css" type="text/css"/> |
|
8 </head> |
|
9 <body bgcolor="#FFFFFF"> |
|
10 <h3>Volatile Variables</h3> |
|
11 <p>(ISO C, §6.7.3) When you declare a volatile variable, the Carbide C compiler takes the following precautions to respect the value of the variable:</p> |
|
12 <ul> |
|
13 <li>The compiler stores commonly used variables in processor registers to produce faster object code. However, the compiler never stores the value of a volatile variable in a processor register.</li> |
|
14 <li>The compiler uses its common sub-expression optimization to compute the addresses of commonly used variables and the results of often-used expressions once at the beginning of a function to produce faster object code. However, every time an expression uses a volatile variable, the compiler computes both the address of the volatile variable and the results of the expression that uses it.</li> |
|
15 </ul> |
|
16 <p>Listing 1 shows an example of volatile variables.</p> |
|
17 <div class="listing"> |
|
18 <h5>Listing 1. Volatile Variables</h5> |
|
19 <p>void main(void)<br /> |
|
20 {<br /> |
|
21 int i[100];<br /> |
|
22 volatile int a, b; /* a and b are not cached in registers. */</p> |
|
23 <p> a = 5;<br /> |
|
24 b = 20;</p> |
|
25 <p> i[a + b] = 15; /* compiler calculates a + b */<br /> |
|
26 i[a + b] = 30; /* compiler recalculates a + b */<br /> |
|
27 }</p> |
|
28 </div> |
|
29 <p>The compiler does not place the value of a, b, or a+b in registers. But it does recalculate a+b in both assignment statements. <br /> |
|
30 </p> |
|
31 <div id="footer">Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. <br>License: <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a></div> |
|
32 |
|
33 |
|
34 </body> |
|
35 </html> |