void.sizeof == 1, not 0

Ali Çehreli acehreli at yahoo.com
Fri Jul 1 13:18:21 PDT 2011


On Fri, 01 Jul 2011 21:18:45 +0200, simendsjo wrote:

> What is contained within this byte?
> (T[0]).sizeof == 0, why isn't void also 0?

void* can point to any data, in which case it is considered to be 
pointing at the first byte of the data. Having a size of one makes it 
point to the next byte when incremented:

    int i;
    void * v = &i;   // first byte
    ++v;             // second byte

Similarly, an empty struct has a size of one:

import std.stdio;

struct S
{}

void main()
{
    assert(S.sizeof == 1);
}

But in that case it is needed to identify S objects from one another just 
by having different addresses. The following array's data will occupy 10 
bytes:

    S[10] objects;
    assert(&(objects[0]) != &(objects[1]));

Ali


More information about the Digitalmars-d-learn mailing list