Thread overview | |||||
---|---|---|---|---|---|
|
July 01, 2011 void.sizeof == 1, not 0 | ||||
---|---|---|---|---|
| ||||
What is contained within this byte? (T[0]).sizeof == 0, why isn't void also 0? |
July 01, 2011 Re: void.sizeof == 1, not 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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
|
July 05, 2011 Re: void.sizeof == 1, not 0 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 01.07.2011 22:18, Ali Çehreli wrote:
> 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
Needed some time to digest your answer, but it makes sense now. Thanks.
|
Copyright © 1999-2021 by the D Language Foundation