Mail Archives: djgpp/2000/05/18/21:47:59
> "A. Sinan Unur" wrote:
> > what do you expect the following code to produce:
> >
> > #include <stdio.h>
> >
> > int main(void) {
> > char c;
> > unsigned int i2;
> > unsigned int i1 = 0x12345678;
> >
> > c = (char) i1;
>
"Alexei A. Frounze" <alex DOT fru AT mtu-net DOT ru> wrote in message
news:<39242D85 DOT 6A95D430 AT mtu-net DOT ru>...
> i1 exceeds valid range. Actually I mean values that can be represented
> by both "<unsigned>int" and "size_t".
i1 is a 32-bit unsigned interger. how does it exceed the values
representable by unsigned int?
>
> > i2 = (unsigned int) c;
>
> Don't overact.
>
> >
> > printf("i1: %8.8x\tc: %2.2x\ti2: %8.8x\n", i1, c, i2);
> >
> > return 0;
> > }
> >
> > do you think i1 == i2?
>
> Am I that stupid???
that wasn't what i was trying to say. all i am trying to say is this :
the problem with casting an object of type size_t to int is the same as the
problem with casting an unsigned int to char. sure enough there may be
platforms where you don't loose any information in the process, but it is
not safe. what if size_t is 64 bits and int is 32 bits? casting how can
casting size_t to int be safe (even disregarding the sign issue, which is
bad enough).
------------------------
A. Sinan Unur
Department of Policy Analysis and Management,
Cornell University, Ithaca, NY 14853, USA.
mailto:sinan AT unur DOT com
http://unur.com/sinan/
- Raw text -