www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array of bit enum

reply =?ISO-8859-1?Q?Val=E9ry?= <valery freesurf.fr> writes:
The result of the following code suggests that arrays of bit enum are 
not packed the way array of plain bit are.
Do we have to avoid enums when we want 1-bit bits ?

Valéry

enum Color : bit { BLACK, WHITE }

const int size = 50;

int main()
{
     List l = new List(null);

     for (int i = 0; i < 10; i++)
         l = new List(l);
     return 0;
}

class List
{
     this(List l)
     {
         b.length = size;
         c.length = size;
         tail = l;
         if (tail)
             // prints "16 64" on my system
             printf("%d %d\n", &tail.b[0] - &b[0], &tail.c[0] - &c[0]);
     }

     List tail = null;
     bit[] b;
     Color[] c;
}
Apr 05 2005
next sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <d2us47$2si6$1 digitaldaemon.com>, =?ISO-8859-1?Q?Val=E9ry?= says...
The result of the following code suggests that arrays of bit enum are 
not packed the way array of plain bit are.
Do we have to avoid enums when we want 1-bit bits ?
Bits are only packed in arrays. They otherwise occupy 1 byte. Sean
Apr 05 2005
parent =?ISO-8859-1?Q?Val=E9ry?= <valery freesurf.fr> writes:
Sean Kelly a écrit :
 In article <d2us47$2si6$1 digitaldaemon.com>, =?ISO-8859-1?Q?Val=E9ry?= says...
 
The result of the following code suggests that arrays of bit enum are 
not packed the way array of plain bit are.
Do we have to avoid enums when we want 1-bit bits ?
Bits are only packed in arrays. They otherwise occupy 1 byte.
Sure. I wonder if enums of type bit are packed in arrays too.
Apr 05 2005
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Valéry wrote:
 The result of the following code suggests that arrays of bit enum are 
 not packed the way array of plain bit are.
 Do we have to avoid enums when we want 1-bit bits ?
<snip> That strikes me as a bug. Here's a simpler testcase: ---------- import std.stdio; enum Colour : bit { BLACK, WHITE } bit[16] b; Colour[16] c; void main() { byte* b0 = cast(byte*) &b[0]; byte* b8 = cast(byte*) &b[8]; byte* c0 = cast(byte*) &c[0]; byte* c8 = cast(byte*) &c[8]; writefln("%d %d", &b8 - &b0, &c8 - &c0); } ---------- Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Apr 06 2005
parent Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am Wed, 06 Apr 2005 17:40:28 +0100:
 Valéry wrote:
 The result of the following code suggests that arrays of bit enum are 
 not packed the way array of plain bit are.
 Do we have to avoid enums when we want 1-bit bits ?
<snip> That strikes me as a bug. Here's a simpler testcase: ---------- import std.stdio; enum Colour : bit { BLACK, WHITE } bit[16] b; Colour[16] c; void main() { byte* b0 = cast(byte*) &b[0]; byte* b8 = cast(byte*) &b[8]; byte* c0 = cast(byte*) &c[0]; byte* c8 = cast(byte*) &c[8]; writefln("%d %d", &b8 - &b0, &c8 - &c0); } ----------
prints: 1 1 (Linux dmd-0.119) Is this OS dependent? Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCVCat3w+/yD4P9tIRArwDAKCmAz7TzFUZTgG3RuC0jRyg3xhx9QCgixT8 bcZJORyIAlwzg3GwjEL6gjs= =1MMU -----END PGP SIGNATURE-----
Apr 06 2005