Re: void vs void* (philosophical question)



Chris Torek <nospam@xxxxxxxxx> writes:
Chris Torek wrote:
(If I were in charge of things, I would probably make "void" an
ordinary object type whose size is zero, so that sizeof(void) ==
0; its value, upon conversion to any other scalar type, would be
zero. Thus:

In article <44A56F01.42670864@xxxxxxxxxxx>
Kenneth Brody <kenbrody@xxxxxxxxxxx> wrote:
How would you handle functions which don't return anything?

void foo()
{
void retval;

... do stuff ...

return retval;
}

Legal, if unnecessarily verbose.

And how would you resolve the use of an uninitialized variable for
the return?

Since objects of type "void" have no bits, it does not matter whether
you initialize them. Their "apparent value" would always be zero.
Assigning any value to a "void" variable throws away its value;

Would you allow comparing voids,

Sure:

void a, b;
if (a == b) /* means "if (0 == 0)" */
...

and void arithmetic?

Yes, with the caveat that any "void" object is alwys zero, so you
cannot divide by a "void":

a + b /* 0 + 0 */
a * 42 /* 0 * 42 */
a / 3 /* 0 / 3 */
a / b /* error, division by zero */

Also, since sizeof(a) == 0 and sizeof(b) == 0, it is possible (but
not required) that &a == &b (or indeed, &a == &anyothervar).

Since sizeof(void) == 0, in this not-quite-C language, arithmetic
on "void *" is also well-defined, and causes nothing to happen:

void *p = malloc(100), *q;
int k = 12;

if (p == NULL) ... handle error ...
q = p + k;
if (q == p)
printf("always true\n");
else
abort();

I think most compilers would warn about arithmetic on "void *",
since the fact that "q = p + k" means the same thing as "q = p"
means that it is not useful to add anything to "p" here. But it
would be allowed.

I think your not-quite-C language is consistent, or can be made so.
But the major difference between real C and your not-quite-C is that
your language allows certain operations that C doesn't -- and as far
as I can tell, few if any of the additional allowed operations are
useful. (Unless I've missed something, which is always a
possibility.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Marshaling Value Types
    ... First, the void* params are concerning if they are pointers to actual data, ... > UINT32 Minor; ... The C# code give me a return value of zero for the> BioAPI_Init call, and both version.major and version.minor are zero. ... >>> I've read that, with the compact framework, MarshalAs is not available> and ...
    (microsoft.public.pocketpc.developer)
  • Re: void vs void* (philosophical question)
    ... ordinary object type whose size is zero, ... its value, upon conversion to any other scalar type, would be ... void retval; ...
    (comp.lang.c)
  • Re: void vs void* (philosophical question)
    ... ordinary object type whose size is zero, ... its value, upon conversion to any other scalar type, would be ... void retval; ... Continuing from where Kenneth Brody left off. ...
    (comp.lang.c)
  • Re: Obvious bug in /sys/i386/include/bus.h (was: bus_at386.h)
    ... >> Adding that extra check for zero transfer length is not going to affect ... static __inline void ...
    (freebsd-hackers)
  • Re: ACBL Bulletin "Bidding Box" scoring?
    ... Are you going to give them zero for not playing SA? ... What do you bid? ... Did you mean to give us an example hand with a spade void instead of a ...
    (rec.games.bridge)