sizeof(void) ?? 
Author Message
 sizeof(void) ??

hello all...

a friend asked me this...

        I need C help.  
        I am taking a class in C. 1 st homework assignment is
        to print out the size in bytes of int, float etc..

        There is a trick question.
        I need to print out the size in bytes of void.
        If void is not a data type how can this be done?

Well, to be honest...

I really have no idea why I cannot compile

                sizeof(void);

Could someone help?

I would like to know for myself.

Tom



Sun, 16 Jul 2000 03:00:00 GMT  
 sizeof(void) ??



Quote:
>hello all...

>a friend asked me this...

>        I need C help.  
>        I am taking a class in C. 1 st homework assignment is
>        to print out the size in bytes of int, float etc..

>        There is a trick question.
>        I need to print out the size in bytes of void.
>        If void is not a data type how can this be done?

It can't. void is an "incomplete" size which means that it has no size.
The language makes it invalid to perform any operation on an incomplete type
that requires knowledge of its size.

Quote:
>Well, to be honest...

>I really have no idea why I cannot compile

>                sizeof(void);

>Could someone help?

sizeof is the most obvious case. Ohter thing you can't do are declare an
array of an incomplete type, define a structure member as a incomplete type
and perform pointer arithmetic on a pointer to an incomplete type.
There are other forms of incomplete type e.g.

extern int a[];

Here a is an array of unknown size of ints. (sizeof a) and so on is invalid

struct foo b;

Here a structure type (struct foo) has been defined but is incomplete because
a member list has not been specified, therefore its size isn't known.

--
-----------------------------------------


-----------------------------------------



Tue, 18 Jul 2000 03:00:00 GMT  
 sizeof(void) ??



Quote:

>    I need C help.  
>    I am taking a class in C. 1 st homework assignment is
>    to print out the size in bytes of int, float etc..

>    There is a trick question.
>    I need to print out the size in bytes of void.
>    If void is not a data type how can this be done?

Lawrence has answered the question (it can't be done) but is your
friend sure it really was 'void', not 'void *'?  "Pointer to void"
is a quite different type which you can take the size of.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Tue, 18 Jul 2000 03:00:00 GMT  
 sizeof(void) ??



Quote:
>hello all...

>a friend asked me this...

>    I need C help.  
>    I am taking a class in C. 1 st homework assignment is
>    to print out the size in bytes of int, float etc..

>    There is a trick question.
>    I need to print out the size in bytes of void.
>    If void is not a data type how can this be done?

Void belongs to the family of incomplete types, so it is not an object type.
Furthermore, whereas other incomplete types have corresponding complete types
with which they are compatible, void has no such type; we say that it cannot
be completed. Only object types can be used to *define* objects, although
declarations with linkage may refer to objects using incomplete types
other than void. Thus ``extern int a[];'' is valid, and may be used to
access an an object defined with external linkage as ``int a[10];''.

Secion 6.3.3.4 of the ISO C standard describes the sizeof operator. In a
paragraph labelled ``Constraints'' it states that ``[t]he sizeof operator
shall not be applied to to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an lvalue
that designates a bit-field object.

Any violation of a requirement stated in such a ``Constraints'' paragraph
is called a constraint violation, and must be diagnosed by the implementation.

That doesn't mean that the implementation has to stop translating the program.
A compiler's designers could produce some value from sizeof(void); as long
as they produce some sort of output that the user recognizes as a diagnostic
message, they are conforming to the requirements.

Quote:

>Well, to be honest...

>I really have no idea why I cannot compile

>            sizeof(void);

Now you know. It's because of a constraint rule in the ISO standard that
defines the language.


Tue, 18 Jul 2000 03:00:00 GMT  
 sizeof(void) ??

Quote:



>>a friend asked me this...

>>        I need C help.  
>>        I am taking a class in C. 1 st homework assignment is
>>        to print out the size in bytes of int, float etc..

>>        There is a trick question.
>>        I need to print out the size in bytes of void.
>>        If void is not a data type how can this be done?

>It can't. void is an "incomplete" size which means that it has no size.
>The language makes it invalid to perform any operation on an incomplete type
>that requires knowledge of its size.

Lawrence is, of course, right.

Another possibility is that the friend misread the question, and
it is really asking for the size of a void*.

While sizeof(void) is not defined, sizeof(void *) is quite
legitimate.

                         - Marc



Tue, 18 Jul 2000 03:00:00 GMT  
 sizeof(void) ??


Quote:

> hello all...

> a friend asked me this...

>         I need C help.  
>         I am taking a class in C. 1 st homework assignment is
>         to print out the size in bytes of int, float etc..

>         There is a trick question.
>         I need to print out the size in bytes of void.
>         If void is not a data type how can this be done?

> Well, to be honest...

> I really have no idea why I cannot compile

>                 sizeof(void);

> Could someone help?

> I would like to know for myself.

Others in this thread have discussed things at great length about
incomplete types and the like.  All that is VERY TRUE.

But, a simple way of THINKING about it is (yes, this is non-standard):
sizeof(void) == 0
This has a few profound effects.
1)  You were told in school that the smallest thing was a char, which has,
by definition, a size of 1.
2)  Funny, things can't be smaller than the smallest thing.  OOOPS, can't
do a sizeof(void).
3)  Attempting to add/subtract an integer to a pointer yields the address
of the Nth element away from the pointer (in addressing terms, it adds to
the base address (in char * terms) N times the sizeof(element).
4)  So you want to add to a 'void *' (pointer to void).  THINKING that
sizeof(void) is zero, will multiply the augned by zero, and you won't add
anything.  OOOPS, can't add to a 'void *' (regardless of what gcc says!!)
5)  A routine 'returns' a 'void' value.  Well these take up NO SPACE
because we have impressed in our head that sizeof(void) == 0, even though
we don't write it down.

Somewhere there is a compiler that will give a result for 'sizeof(void)'.
If it does, all logic (whatever it is) says that a "good answer' probably
is zero.  The correct answer is 'undefined', causing an error.

M{*filter*}of the story:  If you think that sizeof(void) is zero, and then
understand the consequesces of that train of thought, you will probably
'understand'.

--




Tue, 18 Jul 2000 03:00:00 GMT  
 sizeof(void) ??



...

Quote:
>Somewhere there is a compiler that will give a result for 'sizeof(void)'.
>If it does, all logic (whatever it is) says that a "good answer' probably
>is zero.  The correct answer is 'undefined', causing an error.

When not set to -pedantic mode gcc allows you to perform pointer arithmetic
on a void * pointer and it acts as if the type pointed to has size 1. gcc
should be commended as a testbed for possible C language extensions, but
it is important that the mistakes are recognised.

--
-----------------------------------------


-----------------------------------------



Wed, 19 Jul 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. relation between sizeof(void*), sizeof(int)

2. sizeof(void*) versus sizeof(foo*)

3. sizeof void*

4. I wish ANSI-C sizeof (void) == 1 (was Re: preprocessing subterfuge)

5. Question about sizeof (*(void *))

6. a sizeof(void*) question and a -> operator question

7. from void (void*) to void (_cdecl*) (void*)

8. Problem with sizeof of a struct void pointer

9. void *, ptr arithmetic and sizeof()

10. what's the difference between void main(void) and int main(void)

11. difference between void foo(void) and void foo()

12. sizeof (struct) ! = sizeof struct elements

 

 
Powered by phpBB® Forum Software