On 21 Feb 2007 17:24:14 -0800,
in*******@yahoo.com wrote in
comp.lang.c:
Hi experts
Consider this piece of code
unsigned long data = 124; /* Data is actually part of a big struct but
to simplify */
I understand that in a later post you day that the initialization line
above is not correct, and to just assume that you meant to define
"data" as having the type unsigned long.
Unfortunately, in your later post and replies to other respondents you
do not leave enough context to add additional replies.
printf(" %-5llu ", (unsigned long long)(data*1000));
<memory address say 60000000>: 00000000 00000D77 00000000
00000000
0xD77 is 3447
I have no idea what this means, memory dumps are not relevant.
Assume data is at the beginning of memory address 60000000 and this is
a big endian machine (MIPS based).
Why should I assume that? You appear to assume that, and because of
that you assume that the compiler is doing something wrong.
The compiler is doing two load word from 60000000 and 60000000+4 and
then multiplying it by 1000 and then prints 3447000 which is not
expected.
How the compiler chooses to access the contents of lvalues is up to
the compiler. If you are sure it is incorrect you need to contact the
compiler supplier.
My understanding is that the 1000 will default to int
Then it will be promoted to unsigned long (since on our platform long
and int are of same size)
Your understanding is wrong. The type of the integer literal
expression "1000" is int, no default involved. You are also wrong in
thinking that the sizes of int and long have anything to do with the
automatic conversion. When you perform an operation on a signed int
and an unsigned long, the signed int value will be converted to
unsigned long regardless of whether the sizes are the same or
different.
then operation will be carried out in unsigned long (result I mean).
then *result* will be cast to unsigned long long and then it will be
passed to printf
So exactly what are you concerned about? 1000UL * 3447UL = 3447000UL,
which is a value that clearly fits in an unsigned long. The cast
operator converts this unsigned long long, in which it will surely fit
and have the same value.
(not sure how - this is MIPS based platform) and printf is a variadic
function.
I must be missing something or the compiler is broken?
We're missing something, namely the information that you did not
provide. Don't show us a hexadecimal dump of what you think is the
relevant part of memory, that is irreverent.
Are you claiming that the value of "data" is not 3447? How do you
know?
Show us the output of this code:
printf("%lu", data);
printf("%llu", (unsigned long long)(data*1000))
1.Please help me understand what is going on.
2.And how can I fix it the right way.
Even better, show us the structure definition and how the object is
actually accessed in the code, without the simplification.
I have seen cases where an access to a structure member actually
access the wrong bytes of the object. This is most common when people
play games with pointers and make mistakes or have incorrect
assumptions, but there are other possible causes.
--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html