Paste: allocateArray

Author: j
Mode: text
Date: Tue, 26 Apr 2011 21:02:37
Plain Text |
[T, I | Integer?(I)]
allocateArray(static T, count:I) {
    var begin = allocateRawMemory(T, SizeT(count));
    var end = begin + SizeT(count);

    try {
        initializeMemory(begin, end);
    } catch (e) {
        freeRawMemory(begin);
        throw e;
    }
    return CoordinateRange(begin, end);
}

[T]
freeArray(array:CoordinateRange[T]) {
    destroyMemory(begin(array), end(array));
    freeRawMemory(begin(array));
}


main() {
    var xs = allocateArray(Int, 5);

    for (x in xs)
        println(x);

    for (i, x in enumerated(xs))
        x = Int(i);

    println("--");
    for (x in xs)
        println(x);

    freeArray(xs);
}

New Annotation

Summary:
Author:
Mode:
Body: