Ned Konz wrote:
Ron wrote:
Hi Folks. I would have thought that with -Os the code below should work.

static volatile char* Ptr;
   .
   .
static Send(char* p)
{
   if (p)
   {
      while (Ptr != NULL) {;}
      Ptr = p;
      UDR1 = *Ptr++;
   }
}
   .
   .
SIGNAL(SIG_UART1_TRANS)
{
   if (*Ptr == '\0')
      Ptr = NULL;
   else
      UDR1 = *Ptr++;
}

However, the while loop becomes:

       LDS    R24,0x018D
       LDS    R25,0x018E
       SBIW   R24,0x00
       BRNE   PC-0x01

The code is as expected with -O0. Version is 3.4.6. Something I'm
missing?

Probably.

You didn't declare Ptr to be volatile.

Instead you declared Ptr to *point to* a volatile char.

So in the while loop (while you're comparing Ptr to NULL) there's no reason to re-read Ptr because it's not volatile.


That's correct.  In particular,

"volatile char* ptr" parses as "(volatile char) *ptr".

What you want is "char * volatile ptr", meaning that the pointer itself is volatile, not the thing it points to.

A good rule is that a variable declaration should not consist of more than two parts (excluding "static" or "extern", and the variable itself). Any time you need more complex types, use typedefs. Thus:

typedef char *pchar;
volatile pchar Ptr;             // volatile pointer to a char

typedef volatile char vchar;
vchar *Ptr;                     // pointer to volatile char

typedef vchar *pvchar;
volatile pvchar Ptr;            // volatile pointer to volatile char

Then there is (almost!) no chance of getting things wrong.

mvh.,

David


_______________________________________________
AVR-libc-dev mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-libc-dev

Reply via email to