Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: davis at frizzen.com (Davis Johnson)
Subject: Best practice in C to support 64-bit or 32-bit address length with	same source file ?
Date: Sun, 04 May 2008 12:38:32 -0400	[thread overview]
Message-ID: <481DE688.6070001@frizzen.com> (raw)
In-Reply-To: <FB02906B-B6E8-473D-BE92-33632D0F5FD4@raeburn.org>

Ken Raeburn wrote:

> On May 3, 2008, at 16:49, Davis Johnson wrote:
>
>> Rule 1 includes using NULL for null pointer values and not 0. It  
>> also includes using %p to format pointer values. There are probably  
>> some other things I should add.
>
>
> NULL may be defined as 0, though.  In calling a variadic function 
> like  printf, *always* cast NULL (or 0) to the correct pointer type 
> expected  by the function; the compiler should automatically convert 
> NULL or 0  as needed in most other contexts.  In fact, for variadic 
> functions,  you should also make sure your integral-typed arguments 
> are of the  correct types, because passing a 64-bit long where the 
> function  expects a 32-bit int (or vice versa) may cause a mismatch in 
> stack  layout calculations.
>
> Ken
>
> _______________________________________________
> axp-list mailing list
> axp-list at redhat.com
> https://www.redhat.com/mailman/listinfo/axp-list
>
>
In my experience NULL has usually been defined as somthing like ((void 
*)0). In any event it should be defined to be somthing that is safe to 
use as a pointer. It probably shouldn't be defined as just 0 unless int 
and pointers are, in fact, interchangable on that archetecture. Don't 
rule out the possibility of broken archetectures.

K&R C didn't have a void type so older programs (and implementations) 
may not use (or have) it.

The point about variadic functions is well taken. This is related to why 
I sugested being warry of home-rolled variadic parameter schemes. They 
are out there because some of these programs may predate va_arg. For 
non-variadic functions prototypes will make the compiler take care of 
appropriate casts, or give appropriate warnings. For variadic functions 
you need to do the work. GCC allows you to declare functions as 
fprintf-like and it will check functions against the format, but 
otherwise it is up to you.

Casting to the right pointer type is somthing I skimp on because 
everything I've written code for for a long time has had the same 
pointer representation for anything. That is lazy, however. You are 
correct that arguments to variadic functions should be correctly cast.



  reply	other threads:[~2008-05-04 16:38 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 [this message]
2008-05-05  1:25         ` raeburn
2008-05-03 23:03   ` Jay.Estabrook
2008-05-04 15:13     ` raeburn
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