How do you deal with scoped allocations?
Namespace
rswhite4 at googlemail.com
Sat Dec 7 14:32:57 PST 2013
Since my last thread doesn't get much attention I like to ask
here: How did you deal with temporary memory? Let's assume that
the size is only known at runtime.
I have this situation e.g. in Dgame in the capture method: I get
the pixel data from my Window with glReadPixel but it is
reversed. So I have to reverse it again, but I still need at
least temporary memory for one pixel-line which stores the
currently swapped pixels. So how would you solve such a situation?
Since D doesn't offer VLA's and alloca is broken (besides the
ugly syntax),
I use a scoped wrapper (since scope doesn't do the job):
----
struct scoped(A : T[], T) {
T[] arr;
alias arr this;
this(T[] arr) {
this.arr = arr;
writefln("Get %d %s's (ptr = %x)", arr.length, T.stringof,
arr.ptr);
}
~this() {
GC.free(this.arr.ptr);
this.arr = null;
GC.minimize();
}
}
void main() {
// need temp memory
scoped!(int[]) arr = new int[n];
}
----
And what do you use?
More information about the Digitalmars-d
mailing list