Re: void vs void* (philosophical question)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 30 Jun 2006 20:52:29 GMT
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.
.
- References:
- void vs void* (philosophical question)
- From: Giannis Papadopoulos
- Re: void vs void* (philosophical question)
- From: Chris Torek
- Re: void vs void* (philosophical question)
- From: Kenneth Brody
- Re: void vs void* (philosophical question)
- From: Chris Torek
- void vs void* (philosophical question)
- Prev by Date: Re: So what Standard are we working off?
- Next by Date: Re: C sucks at math? (help)
- Previous by thread: Re: void vs void* (philosophical question)
- Next by thread: Re: void vs void* (philosophical question)
- Index(es):
Relevant Pages
|