How to allocate arrays of objects?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 10 20:37:19 PST 2016


On Thursday, 11 February 2016 at 04:31:12 UTC, cy wrote:
>
> Oh, I get it. `as` is an array of 2 pointers to A objects, both 
> pointers set to null. So I need to say like:
>
> as[0..$] = new A();
>
> before accessing .stuff on as[0].

Pedantically, no. It's an array of two class references. I don't 
think it's helpful to think of class references as pointers. 
That's just an implementation detail.

>
> Still no clue why it segfaulted on the allocation though, 
> rather than the statement with the null dereference. Hidden 
> optimization?

You said,

> removing "as[0].stuff = 42" causes the program to stop 
> segfaulting!

Which indicates that was where the segfault was happening. The 
debugger was just stopping before that.


More information about the Digitalmars-d-learn mailing list