From: raeburn at raeburn.org (Ken Raeburn)
Subject: Best practice in C to support 64-bit or 32-bit address length with same source file ?
Date: Sun, 4 May 2008 11:13:30 -0400 [thread overview]
Message-ID: <7BCD3C6D-904D-457A-BD75-2A690931DFF2@raeburn.org> (raw)
In-Reply-To: <481CEF2F.70206@hp.com>
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
prev parent reply other threads:[~2008-05-04 15:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-23 6:23 FC 8 on Alpha question: GNU linker "ld -taso" option? jkmccarthy
2008-04-23 9:24 ` oliver
2008-04-24 1:32 ` rm.riches
2008-05-03 19:20 ` Best practice in C to support 64-bit or 32-bit address length with same source file ? jkmccarthy
2008-05-03 20:49 ` davis
2008-05-04 15:05 ` raeburn
2008-05-04 16:38 ` davis
2008-05-05 1:25 ` raeburn
2008-05-03 23:03 ` Jay.Estabrook
2008-05-04 15:13 ` raeburn [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox