canonical values - an amenity

Garry Wiegand garry at batcomputer.tn.cornell.edu
Thu Nov 20 04:58:46 AEST 1986


The new C incipient-spec has a primitive datatype called "void *". It has
the property that a pointer of any type cast to (void *) and back again
is guaranteed to emerge unharmed.

I use this heavily to pass pointers (to structures) through utility routines
which do not know what type of pointer is coming down, and back again to
action routines which *do* know what type is coming.

What I would like is to be able to do the same thing with *any* kind of
(primitive) value, not merely pointer values!!! (Why do things half-way 
in the language ??)

On many/most machines, "unknown" (somebody was brain-damaged when they
overloaded the word "void"!) would be equivalent to:

	(unknown)char ->    (double)char
	(unknown)pointer -> (double)(long)pointer
	(unknown)short ->   (double)short
	(unknown)long ->    (double)long
	(unknown)int ->     (double)int
	(unknown)float ->   (double)float

I can get away with this because doubles almost always have more
precision in them than any of the other datatypes. I can't
#define "unknown" as "double" because (unknown)pointer will break
on many (broken) compilers. It can't be (double)(long) because
(unknown)float will break. So I need it as a primitive. Ideally
it would just be a 64-bit bitmask internally rather than a double,
to avoid unnecessary conversions.

A fringe benefit is that I can declare *actual variables* to be of
type "unknown", and so avoid having to play with unions of {int, char,
float, double, void *}. 

Tests for equality should still work. Tests for ordering (<, >) would 
work easily on a doubles-implementation but might be tough on bitmasks.

Yah, I know I'm too late. Can I at least have (double)pointer not break,
pleeez? Does the spec already say that (double)pointer is as legit as
and equivalent to (double)(long)(void *)pointer ??

garry wiegand   (garry%cadif-oak at cu-arpa.cs.cornell.edu)



More information about the Comp.lang.c mailing list