[net.lang.c] Context Variables

jeff@isi-vaxa.arpa (Jeffery A. Cavallaro) (12/03/85)

Recent discussion has pointed out that sizeof(int) does not necessarily
== sizeof(char) (what an understatement, blood is still dripping from my
VT100...)

But, is proper to assume:

	sizeof(long) >= sizeof(any scalar data type or pointer)

Yes, I know you can use a union, but unions have this problem of not being
able to be initialized.

If this is OK, it is a very handy way to maintain a context variable to
ensure reentrancy and other such things.

(My specific example...)
I am coding a generic capabilities file parser.  Each capability item in a
definitions is to be described as follows:

struct capitem {
    char *name;		/* Name of item */
    short type;		/* BOOLEAN, STRING, or INTEGER */
    long value;		/* Discovered value */
    long defval;	/* Default value */
}

The value and defval fields would be able to hold a Boolean, char *, or
integer value.  When declaring an input buffer, then default values
could be statically set, and can be assumed to never change.  If this use
of long is not valid and unions must be used, then a routine would be
needed to set the default values for each new instance of a buffer.

I realize that there are other solutions to this problem, but if this use
of long is valid, I like this the best.

				Jeff

jsdy@hadron.UUCP (Joseph S. D. Yao) (12/03/85)

On most "reasonable" machine architectures, any scalar or pointer
data type should fit into a long.  However, in C, a pointer isn't
defined to fit into a long (or be in any way commensurable with
a scalar data type), so you may have problems on some of the more
different architectures.  Supposedly, sizeof(long) >= sizeof(int)
>= sizeof(short), although I have seen someone argue on this net
that it was legitimate to have an int whose size > sizeof(long).
I never saw anyone bother to refute that; but I think it's not
likely.  When they are implemented, a (void*) will hold any
pointer data type, so a union {long; void *;} should hold any
object.  But, to help preserve your own sanity and follow the
Prime Rule ("say what you mean"), you really should make a union
of all the different data types with which you'll be working.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}

franka@mmintl.UUCP (Frank Adams) (12/07/85)

[Not food]

I thought the phrase "scalar data types" included floats and doubles.
Those can certainly be longer than a long.  Double usually is.

As others have noted, there is no guarantee that a (char *) is not longer
than a long, and it could be on reasonable (my opinion) architectures.

Frank Adams                           ihpn4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108

gwyn@BRL.ARPA (VLD/VMB) (12/10/85)

Actually, K&R promises that a long will be large enough to hold
a (char *) without loss of information.  I agree that this is a
mistake, and I believe that X3J11 no longer promises this (I'm
still reviewing the last draft).