New Data Type?

Karl Heuer karl at haddock.ima.isc.com
Thu Mar 22 06:01:40 AEST 1990


In article <9918 at wpi.wpi.edu> oesterle at wpi.wpi.edu (Shawn H. Oesterle) writes:
>The following C data types have incremental sizes of 2^n bytes:
>(unsigned) char, short, and long.

On most implementations.

>It would be nice to have a user definable size for a numerical type...

I see three separate issues in your article.

(a) If the hardware supports a lot of different sizes of integer, it could be
nice to have a way to specify an explicit size instead of having to choose one
of the keywords.  One might use an extension of the bitfield syntax and write
"int foo:64;" for this.  Probably a bad idea, since bitfields are a hack
anyway and since one usually doesn't want a particular bit-size.  Subranges a
la Pascal might be a better choice.

>	void function(maketype mSomewhatBig)  {
>	    maketype mTemporary;
>	    sizeof(mTemporary) = sizeof(mSomewhatBig);
>	    ...

(b) It is sometimes useful to declare one object to be "the same type" as an
existing one, when the name of that type is not readily available.  But
assigning to sizeof() makes my teeth hurt.  Assuming mSomewhatBig has a known
type at compile-time, a much simpler method is to use gcc's "typeof" keyword
(a non-Standard extension): "typeof(mSomewhatBig) mTemporary;"

>	void func2(int iNumber)  {
>	    maketype mTemp;
>	    sizeof(mTemp) = iNumber;
>	    ...

(c) Sometimes the type is not known at compile-time.  (This may have also been
your intent in the earlier example--did you intend it to allow an arbitrary
integral type as parameter, i.e. a generic function?)  The current example is
even harder to implement: how do you generate code for "++mTemp" if you don't
know the type of mTemp at compile time?  You could keep the size information
around at run time and switch on it, I guess.  Or you could just implement
BigNums as in some implementations of Lisp, in which case you don't even have
to bother with assigning to sizeof().  (Just let the number grow on demand.)

>So in accordance with my hardly thought out previous notion on the
>implementation of 'makesize' we have following algorithm in a 'maketype' way:
>	/* compute the factorial of number x */
>	unsigned maketype factorial(unsigned maketype x)  {
>	    unsigned maketype tmp=1;  /* temporary result */
>	    unsigned maketype i;      /* index */
>	    sizeof(i) = sizeof(x);
>	    for(i=1; i<=x; i++) tmp *= i;
>	    return(tmp);
>	}
>Do any programming languages have this ability?

If I correctly understand what you want, you could do this in C++.

I assume that the failure to assign sizeof(tmp) means that you really want it
to be a BigNum, and that you want factorial() to be an overloaded function so
you can call it with different types.  (Useless in this example, actually: you
might as well have i and x be ints, since you won't be able to compute really
huge factorials anyway.)  You could do this in C++.


>"ne zorgas, estu fali<ca!"
         XX        X

That should be "ne zorgu, estu feli<ca!", or simply "ne zorgu, feli<cu!".
(Also, "^c" or "cx" is generally preferred rather than "<c".)

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list