Segmentation fault from using SList in a class?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 6 03:20:33 PDT 2015


On Saturday, 6 June 2015 at 10:16:12 UTC, Tim K. wrote:
> On Saturday, 6 June 2015 at 10:10:15 UTC, Manfred Nowak wrote:
>> x is not initialized.
>>
>> `auto x= new Stack!(int);'
>> will do.
>
> Thank you two.
> But that leads me to another question: Why do I need to 
> initialize x with a "new Stack" but I don't need to initialize 
> p with a "new SList"?

Because `SList` is a struct, which is a value type that is 
allocated where you declare it (here on the stack), while you 
used a class, which is a reference type and has to be allocated 
with `new`.

You can also turn your `Stack` type into a struct, then it works, 
too. (You just need to remove the constructor, which is empty 
anyway.)


More information about the Digitalmars-d-learn mailing list