Re: void vs void* (philosophical question)
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 30 Jun 2006 19:15:34 GMT
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- Follow-Ups:
- Re: void vs void* (philosophical question)
- From: Keith Thompson
- Re: void vs void* (philosophical question)
- 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
- void vs void* (philosophical question)
- Prev by Date: Re: Problems with typedef
- Next by Date: Re: So what Standard are we working off?
- Previous by thread: Re: void vs void* (philosophical question)
- Next by thread: Re: void vs void* (philosophical question)
- Index(es):
Relevant Pages
|