Severe warning here: This is one of the few places with a very severe
and very non-obvious between C and C++. Don't do this if you ever want
to reuse the same code in a C++ program (or you expect that someone
else _might_ want to do this in ten years time).
In one language, an lvalue-to-rvalue conversion takes place, which
accesses *uart_receive. In the other language, no such conversion
takes place, and there is no access. Better write "unsigned char dummy
= *uart_receive; ". (And I don't know which language does the access
and which one doesn't, the distinction is sufficiently obscure that
writing *uart_receive alone is a bug in each language).
You are just plain wrong about this. In both languages the lvalue to
rvalue conversion is performed.
Perhaps you are confused with the way the way the value yielded by the
assignment operator can break volatile objects in C++.
Given:
/* receive and transmit registers share the same address,
as in the ubitquious 16c550 family
*/
volatile unsigned char *uart_rx_tx = /* ... */;
....then:
void uart_xmit(int uch)
{
if ('\r' == (*uart_rx_tx = uch))
{
*uart_rx_tx = '\n';
}
}
The if evaluation requires the value of the assignment operator.
In C, the value of the assignment operator is an rvalue, not an
lvalue. It is the value assigned, that is the rvalue that is actually
written to the destination. It is the value of the right hand side
after conversion, if necessary, to the type of the right hand side. So
it is the value of the int "uch" after truncation to unsigned char.
In C++, on the other hand, the assignment operators yield lvalues, in
this case the object pointed to by "uart_rx_tx". Since the value is
used in the if expression, lvalue to rvalue conversion must be
performed. Since the object is volatile, the conversion must be
performed by reading the value of the object.
So a conforming C++ compiler must first convert the int "uch" to an
unsigned char, write that unsigned char via the pointer, then read
back the value via the pointer to yield the value of the assignment.
In the process, it will not read the value just written to the UART's
transmit register, but will instead read a received character from the
UART's received register.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html