Segmentation fault from using SList in a class?
Tim K. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 6 02:44:36 PDT 2015
Hello!
I just started learning D and I come from a mostly C and C#
background.
Hello World was working, so I figured that I should try something
more complex: a template class and si I went with a simple stack.
Using dmd the code I wrote causes a segmentation fault and I am
not sure why.
import std.stdio;
import std.container.slist;
class Stack(T)
{
SList!(T) s;
this() {}
void push(T element)
{
s.insertFront(element);
}
}
int main()
{
Stack!(int) x;
writeln("1");
SList!(int) p;
writeln("2");
p.insertFront(42);
writeln("3");
x.push(42);
writeln("4");
return 0;
}
This program prints the following into the console:
% dmd test.d && ./test
1
2
3
zsh: segmentation fault ./test
I do not understand why the normal SList works fine but the one
inside the object doesn't. What am I overlooking?
Best regards,
Tim
More information about the Digitalmars-d-learn
mailing list