Re: hii



Umesh wrote:
> Ganga wrote:
> > Please, will anyone explain me difference between 'register' and
> Register keyword for a variable will force the compiler backend to give
> register.
> so by this compiler can acess the variable without store
> instruction(i.e it can use mov instruction).

Not entirely correct. The `register` keyword will /suggest/ to the
compiler that a variable be stored in a register. Most modern compilers
are better at optimising than programmers, and will happily ignore the
`register` keyword.

> > 'volatile' variables
> Volatile Keyword for a variable will prevent variable from
> optimization.

Again, not entirely correct. The `volatile` keyword tells the compiler
that the variable may be changed by something outside the scope of the
current program (other programs in the system, hardware device, cosmic
rays...). Because of this, the compiler cannot be certain of the value
of the `volatile` variable, even if your code did not change it, and
will hence avoid optimisations that rely on that value not changing
between two points in the code.

For example, in the snippet:

int main(void)
{
volatile int i = 5;
int j = 6;

while (3 < i)
{
++j;
}

return 0;
}

The compiler cannot assume that `i` will be 5 when it is tested for the
`while` loop, and hence cannot replace it with the equivalent of `while
(1)`, but has to generate the code for the test. If `i` was not
`volatile`, the compiler could generate the equivavalent of:

int main(void)
{
int j = 6;

endless_loop:
++j;
goto endless_loop;
}

(Note that it could get rid of `i` altogether.)

<snipped not so good an example>

Cheers

Vladimir

.



Relevant Pages

  • Re: Register keyword and "as if" rule...
    ... its way to take a full 10 minutes every time a "register" declared ... Then my compiler is still C compliant, ... keyword if they want their code to run smoothly on all implementations: ... Optimization hints in general are not nearly so important as they were ...
    (comp.lang.c)
  • Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
    ... It doesn't mean that (volatile int*) cast is bad, ... I would agree that fixing the compiler in this case would be a good thing, ... bad register allocation. ... still quite possible to find cases where it did some optimization (in this ...
    (Linux-Kernel)
  • Re: Volatile for shared data protected by critical section
    ... I don't think that it optimizes register variable use across function calls. ... seeing 'volatile' used much with globals in the CE code base. ... Does the current compiler implementation not attempt to optimize variables ... You are right that the global data will be shared by multiple threads ...
    (microsoft.public.windowsce.embedded)
  • Re: volatile in statements (memset_s)
    ... the keyword "volatile", to be used also on statements: ... to tell compiler not to remove or move the following statement. ... memset() usage discussed in the CERT article, ... Your suggestion extends the usage of the "volatile" keyword to affect ...
    (comp.std.c)
  • Re: Volatile for shared data protected by critical section
    ... used for hard spin loops and register accesses, ... 'volatile' used much with globals in the CE code base. ... Does the current compiler implementation not attempt to optimize variables ... You are right that the global data will be shared by multiple threads ...
    (microsoft.public.windowsce.embedded)