Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Void variables

Reply
Thread Tools

Void variables

 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      05-22-2012
On Monday, May 21, 2012 11:22:30 PM UTC+1, Kenny McCormack wrote:
> In article <(E-Mail Removed)>,
> Ian Collins <(E-Mail Removed)> wrote:
> >On 05/22/12 09:53 AM, Raj Pashwar wrote:


> >> I would like to define a variable of void type. However I get compiler
> >> errors:
> >>
> >> void x; // error: variable or field x declared void
> >>
> >> Infact there is a typedef and I want to allow the defined type to be,
> >> void. However, the error is the same.

> >
> >void is an incomplete type. You can't do anything with one, so why
> >would you want one?

>
> I wonder if the goal was to get access to what in some languages (*) is
> referred to as "WORD" - that is, something that isn't really an int or an
> unsigned or anything else - but is just a unit of memory of whatever size
> the native word size on the machine is. I think BCPL (a C ancestor) had
> this.
>
> (*) Or C with MS's Windows defines in place.
>
> --
> Just for a change of pace, this sig is *not* an obscure reference to
> comp.lang.c...




On Monday, May 21, 2012 11:22:30 PM UTC+1, Kenny McCormack wrote:
> In article <(E-Mail Removed)>,
> Ian Collins <(E-Mail Removed)> wrote:
> >On 05/22/12 09:53 AM, Raj Pashwar wrote:
> >> Hello
> >>
> >> I would like to define a variable of void type. However I get compiler
> >> errors:
> >>
> >> void x; // error: variable or field x declared void
> >>
> >> Infact there is a typedef and I want to allow the defined type to be,
> >> void. However, the error is the same.

> >
> >void is an incomplete type. You can't do anything with one, so why
> >would you want one?

>
> I wonder if the goal was to get access to what in some languages (*) is
> referred to as "WORD" - that is, something that isn't really an int or an
> unsigned or anything else - but is just a unit of memory of whatever size
> the native word size on the machine is. I think BCPL (a C ancestor) had
> this.


yes. But that's really *all* it had. int is probably C's nearest approach to a Word.

> (*) Or C with MS's Windows defines in place.


not really
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      05-22-2012
"Kaz Kylheku" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2012-05-21, Ike Naar <(E-Mail Removed)> wrote:
>> On 2012-05-21, Raj Pashwar <(E-Mail Removed)> wrote:
>>> I would like to define a variable of void type.

>>
>> How would such a variable be useful?

>
> Furthermore, among what domain values would it vary?


It wouldn't have any values. Or rather, it would only have one. Since it's
value can then always be implied, it wouldn't actually need any memory.

That means an array of 1000000 (or even SIZE_MAX) void values would use zero
bytes.

I can see this being useful as a marker, in a list of struct fields, or in a
parameter list. So If a struct has fields a,b,c,d,e, then printing them
might give:

(1200, 6, void, 5.6, 0)

when c is void. It doesn't look like it adds any useful information, but
it's presence, and the fact it could be any of the five, does provide
something extra. So if b was void instead, then the output might be:

(1200, void, 6, 5.6, 0)

In both cases, the bit-pattern is the same. Of course, it can only vary
between one compilation and the next, not during at runtime, but that
applies also to a lot of conditional macro preprocessor code.

It can also be made slightly more useful, with a directive to map a void
'value' to a specific numeric or string constant:

#pragma void is 42

void x;

printf("X is %d\n",x);

will display "42".

--
Bartc


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-22-2012
On 5/22/2012 3:15 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Monday, May 21, 2012 10:53:26 PM UTC+1, Raj Pashwar wrote:
>
>> I would like to define a variable of void type. However I get compiler
>> errors:
>>
>> void x; // error: variable or field x declared void
>>
>> Infact there is a typedef and I want to allow the defined type to be,
>> void. However, the error is the same.

>
> there is no such thing as a void type. What do you think it means. If you explain we might be able to tell you how to achieve what you want some other way.


The void type certainly exists, and is described by 6.2.5p19:

"The void type comprises an empty set of values; it is an
incomplete object type that cannot be completed."

ITYM there is no such thing as a void *object*, hence no such thing
as a void variable, an array of voids, and so on.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      05-22-2012
On 22-May-12 04:32, BartC wrote:
> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2012-05-21, Ike Naar <(E-Mail Removed)> wrote:
>>> On 2012-05-21, Raj Pashwar <(E-Mail Removed)> wrote:
>>>> I would like to define a variable of void type.
>>>
>>> How would such a variable be useful?

>>
>> Furthermore, among what domain values would it vary?

>
> It wouldn't have any values. Or rather, it would only have one. Since
> it's value can then always be implied, it wouldn't actually need any
> memory.
>
> That means an array of 1000000 (or even SIZE_MAX) void values would use
> zero bytes.


I don't think that's possible since pointers to different objects must
compare unequal. Likewise, offsets of different members of a struct
must be unique. Finally, given that the representation of a (void *)
must be the same as the representation of a (char *), it would seem most
logical for sizeof(void) to be 1, with zero value bits and CHAR_BIT
padding bits.

> It can also be made slightly more useful, with a directive to map a void
> 'value' to a specific numeric or string constant:
>
> #pragma void is 42
>
> void x;
>
> printf("X is %d\n",x);
>
> will display "42".


I have a serious problem with that. AFAIK, every other type must be
able to represent the value 0, and (void) should as well. It's just
that a (void) couldn't represent any _other_ value.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-22-2012
On 05/22/2012 05:32 AM, BartC wrote:
> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2012-05-21, Ike Naar <(E-Mail Removed)> wrote:
>>> On 2012-05-21, Raj Pashwar <(E-Mail Removed)> wrote:
>>>> I would like to define a variable of void type.
>>>
>>> How would such a variable be useful?

>>
>> Furthermore, among what domain values would it vary?

>
> It wouldn't have any values. Or rather, it would only have one.


You were right the first time. "one value" is one more than the number
specified by the standard: "The void type comprises an empty set of
values" (6.2.5p19).
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      05-22-2012


"James Kuyper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 05/22/2012 05:32 AM, BartC wrote:
>> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 2012-05-21, Ike Naar <(E-Mail Removed)> wrote:
>>>> On 2012-05-21, Raj Pashwar <(E-Mail Removed)> wrote:
>>>>> I would like to define a variable of void type.
>>>>
>>>> How would such a variable be useful?
>>>
>>> Furthermore, among what domain values would it vary?

>>
>> It wouldn't have any values. Or rather, it would only have one.

>
> You were right the first time. "one value" is one more than the number
> specified by the standard: "The void type comprises an empty set of
> values" (6.2.5p19).


An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.

Since a 'void' type would have zero bits, that suggests it would have 2**0
values, or just one.

--
Bartc

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      05-22-2012
On Tue, 22 May 2012 17:08:42 +0100, "BartC" <(E-Mail Removed)> wrote:

>
>
>"James Kuyper" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> On 05/22/2012 05:32 AM, BartC wrote:
>>> "Kaz Kylheku" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> On 2012-05-21, Ike Naar <(E-Mail Removed)> wrote:
>>>>> On 2012-05-21, Raj Pashwar <(E-Mail Removed)> wrote:
>>>>>> I would like to define a variable of void type.
>>>>>
>>>>> How would such a variable be useful?
>>>>
>>>> Furthermore, among what domain values would it vary?
>>>
>>> It wouldn't have any values. Or rather, it would only have one.

>>
>> You were right the first time. "one value" is one more than the number
>> specified by the standard: "The void type comprises an empty set of
>> values" (6.2.5p19).

>
>An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.
>
>Since a 'void' type would have zero bits, that suggests it would have 2**0
>values, or just one.


You are assuming the function is continuous over the number of bits.
The standard clearly states it is not continuous at the value N = 0.

--
Remove del for email
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-22-2012
On 05/22/2012 12:08 PM, BartC wrote:
>
>
> "James Kuyper" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 05/22/2012 05:32 AM, BartC wrote:

[Re: an object of void type]
>>> It wouldn't have any values. Or rather, it would only have one.

>>
>> You were right the first time. "one value" is one more than the number
>> specified by the standard: "The void type comprises an empty set of
>> values" (6.2.5p19).

>
> An 8-bit type has 256 distinct values. An N-bit one would have 2**N values.
>
> Since a 'void' type would have zero bits, that suggests it would have 2**0
> values, or just one.


The standard does not say that objects of void type require zero bits;
it says that objects cannot have that type. It does say that "The void
type comprises an empty set of values", so inverting your argument, that
would suggest that an object of void type should contain -INFINITY bits.
That presents certain practical problems , so I suppose that's one
reason why we're not allowed to create such an object.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      05-22-2012


"James Kuyper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 05/22/2012 12:08 PM, BartC wrote:
>>
>>
>> "James Kuyper" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 05/22/2012 05:32 AM, BartC wrote:

> [Re: an object of void type]
>>>> It wouldn't have any values. Or rather, it would only have one.
>>>
>>> You were right the first time. "one value" is one more than the number
>>> specified by the standard: "The void type comprises an empty set of
>>> values" (6.2.5p19).

>>
>> An 8-bit type has 256 distinct values. An N-bit one would have 2**N
>> values.
>>
>> Since a 'void' type would have zero bits, that suggests it would have
>> 2**0
>> values, or just one.

>
> The standard does not say that objects of void type require zero bits;
> it says that objects cannot have that type. It does say that "The void
> type comprises an empty set of values", so inverting your argument, that
> would suggest that an object of void type should contain -INFINITY bits.
> That presents certain practical problems , so I suppose that's one
> reason why we're not allowed to create such an object.


So a void type is just the empty set of values { }.

But the empty set is itself a value. So the empty set must contain itself:
{{{...}}}.

There seems to be some paradox here.

--
Bartc

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-22-2012
On 05/22/2012 12:30 PM, BartC wrote:
>
>
> "James Kuyper" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...

....
>> The standard does not say that objects of void type require zero bits;
>> it says that objects cannot have that type. It does say that "The void
>> type comprises an empty set of values", so inverting your argument, that
>> would suggest that an object of void type should contain -INFINITY bits.
>> That presents certain practical problems , so I suppose that's one
>> reason why we're not allowed to create such an object.

>
> So a void type is just the empty set of values { }.
>
> But the empty set is itself a value. So the empty set must contain itself:
> {{{...}}}.
>
> There seems to be some paradox here.


No, just a problem with your logic.

You could define a type that includes the empty set among it's
representable values - such a type could be useful for applications
involving set theory. However, the C standard says that the set of
values of void type is empty, so in particular, it cannot include the
empty set itself as a value.

Maybe that argument would be clearer if I used a non-void type for a
comparison. The set of values that comprise the 'int' type is the
integers from INT_MIN to INT_MAX; that set is not, itself, one of the
values contained in that set; all of the members of that set are
integers, none of them are sets of integers.

Your conclusion that "the empty set must contain itself" is wrong; it
wouldn't be "the empty set" if that conclusion were true. There are
people here with a stronger mathematical background than I have; I hope
that one of them can explain more precisely than I can what's wrong with
that conclusion.

You don't explain how you went from your premise to your conclusion.
Even if the premise were correct, I can't come up with any valid
argument connecting it to your conclusion.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
returning a void (*)(void) Sergio C++ 6 01-05-2005 08:30 PM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments