The answer is very simple - you *should* be pernickety about variables that
you don't want optomised out.  The term "volatile" tells the compiler that a
variable's value may change without it knowing about it, or have other side
effects - thus the compiler must read or write it exactly as often as
specified in the source code.  This is particularly important for variables
that are changed (or read - it works both ways) within interrupt routines.
It also applies to hardware registers, which are all effectively volatile in
the header files.

However, making all variables "volatile" by default would be using a cannon
to swat a fly.  The resulting code would be poor, full of unnecessary reads
and writes to memory and with the compiler unable to perform all its neat
optomisation tricks.  Declare variables as volatile when necessary, but not
otherwise.


> I'm having a lot of trouble with the way GCC treats various objects. In
> particular, if I want to pass falgs about in my program, unless I'm very
> pernickety it optimeses them out, assuming they aren't wanted. For
> example: (ticks is incremented by an interrupt)
>
> void wait_ticks( unsigned int nticks)
> {
> unsigned int t;
>
> t = ticks;
>
> while( (ticks-t) < nticks)
>  ;
> }
>
> If I don't declare ticks as volatile, I get this:
>
> 000047d4 <wait_ticks>:
>     47d4: 0e 43             clr r14 ;
>     47d6: 0e 9f             cmp r15, r14 ;
>     47d8: fe 2b             jnc $-2      ;abs dst addr 0x47d6
>     47da: 30 41             ret
>
> which just hangs.
>
> Is there any way of forcing the compiler to assume everything is
> volatile? Why does it do this anyway?
>
> Paul Burke
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
>
http://ad.doubleclick.net/clk;4699841;7576301;v?http://www.sun.com/javavote
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>
>



Reply via email to