20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Type Qualifiers<br />

379<br />

register <strong>in</strong>t <strong>in</strong>dex;<br />

register char *textPtr;<br />

Both local variables and formal parameters can be declared as register variables.The<br />

types of variables that can be assigned to registers vary among mach<strong>in</strong>es.The basic data<br />

types can usually be assigned to registers, as well as po<strong>in</strong>ters to any data type.<br />

Even if your compiler enables you to declare a variable as a register variable, it is<br />

still not guaranteed that it will do anyth<strong>in</strong>g with that declaration. It is up to the<br />

compiler.<br />

You might want to also note that you cannot apply the address operator to a<br />

register variable. Other than that, register variables behave just as ord<strong>in</strong>ary automatic<br />

variables.<br />

The volatile Qualifier<br />

This is sort of the <strong>in</strong>verse to const. It tells the compiler explicitly that the specified variable<br />

will change its value. It’s <strong>in</strong>cluded <strong>in</strong> the language to prevent the compiler from<br />

optimiz<strong>in</strong>g away seem<strong>in</strong>gly redundant assignments to a variable, or repeated exam<strong>in</strong>ation<br />

of a variable without its value seem<strong>in</strong>gly chang<strong>in</strong>g. A good example is to consider an<br />

I/O port. Suppose you have an output port that’s po<strong>in</strong>ted to by a variable <strong>in</strong> your program<br />

called outPort. If you want to write two characters to the port, for example an O<br />

followed by an N, you might have the follow<strong>in</strong>g code:<br />

*outPort = 'O';<br />

*outPort = 'N';<br />

A smart compiler might notice two successive assignments to the same location and,<br />

because outPort isn’t be<strong>in</strong>g modified <strong>in</strong> between, simply remove the first assignment<br />

from the program.To prevent this from happen<strong>in</strong>g, you declare outPort to be a<br />

volatile po<strong>in</strong>ter, as follows:<br />

volatile char *outPort;<br />

The restrict Qualifier<br />

Like the register qualifier, restrict is an optimization h<strong>in</strong>t for the compiler. As such,<br />

the compiler can choose to ignore it. It is used to tell the compiler that a particular<br />

po<strong>in</strong>ter is the only reference (either <strong>in</strong>direct or direct) to the value it po<strong>in</strong>ts to throughout<br />

its scope.That is, the same value is not referenced by any other po<strong>in</strong>ter or variable<br />

with<strong>in</strong> that scope.<br />

The l<strong>in</strong>es<br />

<strong>in</strong>t * restrict <strong>in</strong>tPtrA;<br />

<strong>in</strong>t * restrict <strong>in</strong>tPtrB;<br />

tell the compiler that, for the duration of the scope <strong>in</strong> which <strong>in</strong>tPtrA and <strong>in</strong>tPtrB are<br />

def<strong>in</strong>ed, they will never access the same value.Their use for po<strong>in</strong>t<strong>in</strong>g to <strong>in</strong>tegers <strong>in</strong>side<br />

an array, for example, is mutually exclusive.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!