Thread overview
Odd behaviour with dynamic array within a struct
Aug 08, 2004
Brad Beveridge
Aug 08, 2004
Regan Heath
Aug 09, 2004
Brad Beveridge
August 08, 2004
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;
}
August 08, 2004
"Brad Beveridge" <brad.beveridge@somewhere.com> escribió en el mensaje
news:cf4hbt$2lig$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;
| }

D is not like C in this aspect. What you have as version(ok) is the correct way
in D.

-----------------------
Carlos Santander Bernal


August 08, 2004
On Sun, 8 Aug 2004 08:39:28 -0500, Carlos Santander B. <carlos8294@msn.com> wrote:
> "Brad Beveridge" <brad.beveridge@somewhere.com> escribió en el mensaje
> news:cf4hbt$2lig$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;
> | }
>
> D is not like C in this aspect. What you have as version(ok) is the correct way in D.

Yeah, but surely the line:
  a.fileData.someval=66;

should be illegal if version=ok is not defined?

What about:
  printf("someval %i\n", a.fileData.someval);

does that make any sense if version=ok is not defined?

Regards,
Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
August 09, 2004
Thanks for the replies guys.  I actually ment to post this to the main newsgroup, but made a mistake.  Walter says it is a bug BTW :)

Cheers
Brad