|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
digitalmars.D.dtl - Odd behaviour with dynamic array within a struct
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;
}
Aug 07 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
Aug 08 2004
On Sun, 8 Aug 2004 08:39:28 -0500, Carlos Santander B. <carlos8294 msn.com> wrote: Aug 08 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 Aug 08 2004
|