On Thursday, 31 July 2014 at 18:30:41 UTC, Anonymous wrote:
module test;
import std.stdio;

class buffer(T, size_t sz) {
        auto arr = new T[sz];

This allocates an array with `sz` elements once _at compile time_, places it somewhere into the executable, and uses its address as the default initializer for the member `arr`. All instances of `buffer` (with the same template parameters) that you create and don't change `arr` have it point to the same memory.

Use this instead:

    class buffer(T, size_t sz) {
        T[sz] arr;
        enum end = sz-1;
    }

This embeds `arr` into the class, instead of making it a reference to a dynamic array. If you want the latter, use this:

    class buffer(T, size_t sz) {
        T[] arr;
        enum end = sz-1;
        this() {
            arr = new T[sz];
        }
    }

Reply via email to