It's a bug.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf4n0j$2odm$1 digitaldaemon.com...
Hi all. I am working with DMD 0.98 on linux. I've just run across
something that is a little non-intuitive. An anonymous struct within a
struct (or class) will allocate storage, and allow you to use members
within that struct - this is expected. However, a named struct within a
struct will not allocate storage, but its members are still happily
accessible. This more or less gives the effect of a union. Is this
intended or a nasty side-effect? This took me a while to understand - I
think it's a bit of a trap for new D programmers. Comments?
There is some example code below.
Cheers
Brad
//version=ok; // uncomment to fix
struct A
{
version(ok){
struct _fileData{
int someval;
}
_fileData fileData;
} else {
struct fileData{
int someval;
}
}
int [] data;
}
void printInfo(A a)
{
printf("addr %x sizeof %i\n", &a, a.sizeof);
printf("someval %i\n", a.fileData.someval);
printf("length of data %i\n", a.data);
}
int main(char[][] arg)
{
int [] d;
A a;
a.fileData.someval=66;
printInfo(a);
a.data.length= 550;
printInfo(a);
return 0;
}
OK, cool. What will the fixed behaviour be like? I think I would
personally prefer that named structs within structs act just like anon
structs & allocate storage. For those of you wondering why this is useful,
consider this trivial example.
struct BMP
{
struct fileData
{
uint width;
uint height;
}
char [] data;
}
BMP bmp;
file.readExact(&bmp, bmp.fileData.sizeof)
bmp.data.length = bmp.fileData.width * bmp.fileData.height;
Now if there was some way to breakout the internal struct names as if
fileData were anonymous, I would be really happy. Actually, thinking about
it I guess I could actually have fileData as anonymous and use
bmp.data.offset as the read in length.
Cheers
Brad
Walter wrote:
It's a bug.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf4n0j$2odm$1 digitaldaemon.com...
Hi all. I am working with DMD 0.98 on linux. I've just run across
something that is a little non-intuitive. An anonymous struct within a
struct (or class) will allocate storage, and allow you to use members
within that struct - this is expected. However, a named struct within a
struct will not allocate storage, but its members are still happily
accessible. This more or less gives the effect of a union. Is this
intended or a nasty side-effect? This took me a while to understand - I
think it's a bit of a trap for new D programmers. Comments?
There is some example code below.
Cheers
Brad
Hmm, I don't know what I was thinking with this offset business, is there
anyway I can find how far from the start of a structure a particular member
is? I thought I had seen the struct.member.offset property before.
Brad
structname.member.offset ought to do it.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf74pf$bco$1 digitaldaemon.com...
Hmm, I don't know what I was thinking with this offset business, is there
anyway I can find how far from the start of a structure a particular
member
is? I thought I had seen the struct.member.offset property before.
Brad
Yep - that worked. I was trying to use structure_instance.member.offset.
Thanks.
Brad
Walter wrote:
structname.member.offset ought to do it.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf74pf$bco$1 digitaldaemon.com...
Hmm, I don't know what I was thinking with this offset business, is there
anyway I can find how far from the start of a structure a particular
member
is? I thought I had seen the struct.member.offset property before.
Brad
Anonymous structs allocate storage, named structs simply create a name for a
type.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf74c7$bau$1 digitaldaemon.com...
OK, cool. What will the fixed behaviour be like? I think I would
personally prefer that named structs within structs act just like anon
structs & allocate storage. For those of you wondering why this is
useful,
consider this trivial example.
struct BMP
{
struct fileData
{
uint width;
uint height;
}
char [] data;
}
BMP bmp;
file.readExact(&bmp, bmp.fileData.sizeof)
bmp.data.length = bmp.fileData.width * bmp.fileData.height;
Now if there was some way to breakout the internal struct names as if
fileData were anonymous, I would be really happy. Actually, thinking
about
it I guess I could actually have fileData as anonymous and use
bmp.data.offset as the read in length.
Cheers
Brad
Walter wrote:
It's a bug.
"Brad Beveridge" <brad.beveridge somewhere.com> wrote in message
news:cf4n0j$2odm$1 digitaldaemon.com...
Hi all. I am working with DMD 0.98 on linux. I've just run across
something that is a little non-intuitive. An anonymous struct within a
struct (or class) will allocate storage, and allow you to use members
within that struct - this is expected. However, a named struct within a
struct will not allocate storage, but its members are still happily
accessible. This more or less gives the effect of a union. Is this
intended or a nasty side-effect? This took me a while to understand - I
think it's a bit of a trap for new D programmers. Comments?
There is some example code below.
Cheers
Brad