Re: ANSI C clarifications, with citations (was Re: [patch-2.4

Horst von Brand (vonbrand@inf.utfsm.cl)
Tue, 27 Jun 2000 15:47:50 -0400


Jesse Pollard <pollard@tomcat.admin.navo.hpc.mil> said:
> "Albert D. Cahalan" <acahalan@cs.uml.edu>:

[...]

> > Moderately portable code assumes a sane compiler and sane hardware.
> > You may assume:
> >
> > 1. The "char" type is 8 bits. It might be unsigned though. :-/
> > 2. sizeof(short) == 2
> > 3. sizeof(int) == 4 (for real Linux, not the 8088 hack)
> > 4. sizeof(long) == sizeof(void *)
> > 5. (sizeof(long) == 4) | (sizeof(long) == 8)
> > 6. sizeof(long long) == 8 (good for 10 years at least)
> > 7. You can freely cast between any two pointer types
> > 8. You can freely cast between long and any pointer type
> > 9. (long)(int*)(long)foo == (long)(char*)(int*)(long)foo
> > 10. signed integers are represented in two's complement form
> > 11. integers wrap around instead of causing faults
> > 12. assuming "good" struct layout, padding only occurs at the end
> > 13. ... that padding won't happen if you supply a multiple of 16 bytes

[...]

> #8 and #9 may be assumptions, but I think they are BAD assumptions and
> should be avoided.

C guarantees _some_ unsigned type is at least as long as a void *, and all
pointers can be converted to void * and back without problems.

> I believe the Sparc supports 64 bit pointers, as does the Alpha, yet long
> is still 4 bytes. This violates assumption 8 (both 64 bit mode of
> course).

On Alpha, int is 32 bits and long is 64 (and pointers are 64 bits).

[...]

> I personally don't like #11. Signed ints could cause a fault, but unsigned
> ints shouldn't (in my opinion).

This is what the standard says, AFAIU.

-- 
Dr. Horst H. von Brand                       mailto:vonbrand@inf.utfsm.cl
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/