www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why are std.bitmanip.bitfields so big ?

reply wjoe <invalid example.com> writes:
 From the API documentation:

 Create a bitfield pack of eight bits, which fit in one ubyte.
 [...]

 struct A
 {
     mixin(bitfields!(
        bool, "flag1",    1,
        bool, "flag2",    1,
        uint, "",         6));
 }
 
 A a;
 writeln(a.flag1); // 0
 a.flag1 = 1;
 writeln(a.flag1); // 1
 a.flag1 = 0;
 writeln(a.flag1); // 0
 writeln(a.sizeof);
 Application output

 false
 true
 false
 16
I would expect a sizeof 1. Why is its size 16 ? Source: https://dlang.org/library/std/bitmanip/bitfields.html
Jul 27 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/27/20 5:49 AM, wjoe wrote:
 struct A
 {
      mixin(bitfields!(
         bool, "flag1",    1,
         bool, "flag2",    1,
         uint, "",         6));
 }
Is this inside a function? If so, put `static` on it. What you are seeing is the 8-byte frame pointer that comes from inner structs so you can access stack variables inside the struct. -Steve
Jul 27 2020
parent reply wjoe <invalid example.com> writes:
On Monday, 27 July 2020 at 12:52:53 UTC, Steven Schveighoffer 
wrote:
 On 7/27/20 5:49 AM, wjoe wrote:
 struct A
 {
      mixin(bitfields!(
         bool, "flag1",    1,
         bool, "flag2",    1,
         uint, "",         6));
 }
Is this inside a function? If so, put `static` on it. What you are seeing is the 8-byte frame pointer that comes from inner structs so you can access stack variables inside the struct. -Steve
It was run on the doc page. I suppose the examples are wrapped in a unittest block? Anyways, I appreciate your explanation.
Jul 28 2020
parent reply MoonlightSentinel <moonlightsentinel disroot.org> writes:
On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
 It was run on the doc page. I suppose the examples are wrapped 
 in a unittest block?
Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314
Jul 28 2020
next sibling parent wjoe <invalid example.com> writes:
On Tuesday, 28 July 2020 at 09:46:01 UTC, MoonlightSentinel wrote:
 On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
 It was run on the doc page. I suppose the examples are wrapped 
 in a unittest block?
Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314
Thank you.
Jul 28 2020
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/28/20 5:46 AM, MoonlightSentinel wrote:
 On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
 It was run on the doc page. I suppose the examples are wrapped in a 
 unittest block?
Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/ itmanip.d#L293-L314
It doesn't necessarily need to be in a unittest block (AFAIK, the example executer isn't actually running the unittests of phobos), but it does need to be inside a function, because you have executable code there. Either way, yes, it's run inside a function, so you need to apply static to the struct to avoid the frame pointer. I tested that, and it prints 1 instead of 16. -Steve
Jul 28 2020
parent wjoe <invalid example.com> writes:
On Tuesday, 28 July 2020 at 13:00:12 UTC, Steven Schveighoffer 
wrote:
 On 7/28/20 5:46 AM, MoonlightSentinel wrote:
 On Tuesday, 28 July 2020 at 09:28:27 UTC, wjoe wrote:
 It was run on the doc page. I suppose the examples are 
 wrapped in a unittest block?
Indeed, see https://github.com/dlang/phobos/blob/cd2ba0d2c378a893ec0eaefc57b87d0770a1990c/std/bitmanip.d#L293-L314
It doesn't necessarily need to be in a unittest block (AFAIK, the example executer isn't actually running the unittests of phobos), but it does need to be inside a function, because you have executable code there. Either way, yes, it's run inside a function, so you need to apply static to the struct to avoid the frame pointer. I tested that, and it prints 1 instead of 16. -Steve
My reasoning was that a unittest is essentially a function and it's incredibly convenient for such things but I hadn't really checked prior to MoonlightSentinel's reply. Thanks again :)
Jul 28 2020