Help to find crash in simple stack type?
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 12 06:26:21 PDT 2014
I've created a simple stack type using calloc/free which seems to
work nicely. Then instead of using C functions i've tried to
implement the same type using the GC. However i'm experiencing a
crash. I've been staring at this snippet for hours now any help
would be appreciated. This is a simplified snippet showing the
crash:
import std.stdio;
import core.memory;
class Stack(T)
{
private T* _data;
private T* _pointer;
private immutable size_t _minimumSize;
private size_t _size;
private size_t _count;
public this()
{
this._minimumSize = 32_000;
this._size = this._minimumSize;
this._data = cast(T*)GC.calloc(this._size, GC.BlkAttr.NO_MOVE);
this._pointer = this._data;
this._pointer--;
}
public void push(T value)
{
this._pointer++;
if ((this._size / T.sizeof) < (this._count + 1))
{
this._size *= 2;
writefln("realloc to %s bytes", this._size);
this._data = cast(T*)GC.realloc(this._data, this._size,
GC.BlkAttr.NO_MOVE);
this._pointer = (this._data + this._count);
}
this._count++;
*this._pointer = value;
}
}
unittest
{
auto stack = new Stack!(int);
for (int x = 1; x <= 300_000 ; x++)
{
stack.push(x);
}
}
It seems to crash when the loop iteration is at about 260,000 and
i've no idea why. I'm using Ubuntu 64bit latest DMD.
More information about the Digitalmars-d-learn
mailing list