Is this a bug?

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 28 03:03:38 PDT 2014


Andrey:

> Could anyone please explain to me, why do I have error message 
> on this piece of code?

In this thread you are doing some mistakes. This code seems OK:

alias TData = short;
alias TArray = TData[100];

struct MyStruct {
     TArray* arrPtr;
}

void main() {
     MyStruct* t3 = new MyStruct(new TArray[1].ptr);
     TData data0 = (*t3.arrPtr)[99];
}


D is working as designed. (The only small problem is that you 
can't "new" a fixed size array, so I have had to allocate a 
dynamic array of TArray of length 1 and take the pointer to its 
first item. Another alternative solution is to wrap TArray into a 
struct.)

Such kind of code is possible in D, but it should be quite 
uncommon in library code (and it can't be marked safe). There are 
usually simpler ways to do similar things in D. So perhaps there 
are ways to simplify your data structure (here I have kept the 
same structure you have used in the example).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list