[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Best practice in C to support 64-bit or 32-bit address length with same source file ?



On May 3, 2008, at 19:03, Jay Estabrook wrote:
  Modern C on most machines now establishes:

   sizeof(long integer) == sizeof(pointer)

  so if you MUST live with a program in which is is common to go
  back and forth between something declared "int" and a pointer,
  changing the variable declared "int" to "long" often will do the
  trick.

Not all systems do that, though. On 64-bit Windows, for example, sizeof(int)==sizeof(long)<sizeof(void*). I don't have any UNIX examples to point to offhand, but I don't think this is a safe assumption to make in general.

If you want to mix pointers and integral types like that, I'd say look for "intptr_t", which should be defined on any system that's up to fairly current C standards.

Another area where I've occasionally seen problems is assuming "int" and "long" are the same when using pointers, or that "long" can be used when you need a type that's exactly 32 bits. If you have a long* in one place, and elsewhere you have an int* or a pointer to an array of four bytes, and you ignore the compiler warnings (or don't require that function prototypes be visible at call sites), the code indirecting through the pointer can use more or fewer bytes than were really being provided. So: Turn on compiler warnings for missing prototypes and pointer type mismatches, and fix those warnings.

Ken


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]