Topic: Void and zero-size object( Realizable )


Author: tinct@163.com (Tinct)
Date: Wed, 26 Sep 2001 14:42:47 GMT
Raw View
I have read the discussion "Question about vacancy struct" in
comp.lang.c++.moderated. I have just tested the usages of void and
then I am thinking about their relations.

A vacany struct is a zero-size struct without data members and virtual
functions like
   struct vacancy
   { // no data members
        void foo(void);
   };
One can declare:
   vacancy v;
   void f( vacancy  x);
And, sizeof(v) != 0. There are actually memory allocation and
parameters pushing into stacks.

Two opinions. One is sizeof(v) should be zero and therefore no memory
alloc and stack pushing! The other is sizeof(v) should be non-zero and
therefore remain classical usages. The focus is how to distinguish
vacant object if they are zero-size.

The former is more effective both in speed and size. Let's consider
it.
1. sizeof(v) return 0
2. no codes for x' memory alloc
3. no codes for parameter pushing for f

An good idea! When using any object of vacant struct, the compiler
call its method directly. ( The object of vacant is something dumb. )
Then how about pointer, array and reference of zero-size object? x, y
are 2 objects, how about &x == &y ? How to distinguish them by
address?
Also here are two opioions:
1. We need NOT to distinguish them for they are identical. The address
is meaningful to programmers(observers) and is meaningless to the
object itself. For a long time, progammers take it for granted to
treat the pair (object, the object's address) as object itself. (We
all use computers with RAM/ROM.) But object itself need NOT appear
somewhere.
   SO, we can give an compiler error when getting the address of a
zero-size object. Or, we can return 0 ( or any other value ) for the
address.
2. We really need to distinguish them.
   So, we should let size_of(v) not 0!
OR: we could allocate a different address for each vacant object but
no memory for them. It is actual realizable because the compiler has
enough address to do it. For nny vacant object has its literal
identifier such as x, y[0], We can give it an address(or:
numeric-format identifier) such as 0x0001, 0x0002. No actual memory
for them. And, memcpy should work for the copy size = 0.

Unsolved Question:
   vacant xa [ 100 ] ; // sizeof(xa)/sizeof(xa[0])= 0/0 error!
   vacant * ap = &xa[0];
   pa ++; // remain &xa[0]
To avoid these quesion, it is legal to declare "vacant x" and "(vacant
*) px".
But the compilers forbid tring to get address of vacant object and
give warning to using +/-/++/-- of these pointer(e.g. "Warning:
Pointer value unchanged after +/-/++/--").

Therefore void should not be a special type in c++. Just a build-in
common zero-size type.

Zero-size object is realizable and effective. And, Why not?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Wed, 26 Sep 2001 15:11:32 GMT
Raw View

Tinct wrote:

> A vacany struct is a zero-size struct without data members and virtual
> functions like
>    struct vacancy
>    { // no data members
>         void foo(void);
>    };
> One can declare:
>    vacancy v;
>    void f( vacancy  x);
> And, sizeof(v) != 0. There are actually memory allocation and
> parameters pushing into stacks.

For conveniece and consistancy, the C++ standard mandates all objects
must have size of at least 1.  I have no clue what you are talking about
with regard to "stack pushing".  There are a good reasons
for this.  Henerally, nobody had a particularly good reason why a
zero sizeof would be useful even if it could happen.

About the only time this is a problem is when the "empty" object is contained
inside another.  This potentially is a small amount of wasted space (could add
up if your paradigm has an object containing a lot of them).   Technically, a
savvy compiler is allowed to optimize this down to a compact size (though I don't
know of any that do).

> 3. no codes for parameter pushing for f

What parameter pushing are we talking about.

> 1. We need NOT to distinguish them for they are identical. The address
> is meaningful to programmers(observers) and is meaningless to the
> object itself.

This is best served by just using a single static object possibly with
only static member functions (if it the object is really unimportant,
why are you bothering with carrying around the "this" concept).

>    So, we should let size_of(v) not 0!
> OR: we could allocate a different address for each vacant object but
> no memory for them. It is actual realizable because the compiler has
> enough address to do it. For nny vacant object has its literal
> identifier such as x, y[0], We can give it an address(or:
> numeric-format identifier) such as 0x0001, 0x0002. No actual memory
> for them. And, memcpy should work for the copy size = 0.

This would involve a substantial amount additional code to substitute
the bogus identifiers when a few bytes of allocated memory would allow
it to be done as efficiently as anything else in C++.

>
> Unsolved Question:
>    vacant xa [ 100 ] ; // sizeof(xa)/sizeof(xa[0])= 0/0 error!
>    vacant * ap = &xa[0];
>    pa ++; // remain &xa[0]
> To avoid these quesion, it is legal to declare "vacant x" and "(vacant
> *) px".
> But the compilers forbid tring to get address of vacant object and
> give warning to using +/-/++/-- of these pointer(e.g. "Warning:
> Pointer value unchanged after +/-/++/--").
>

More special cases to the language with no apparent benefit.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]