Unexpected memory reuse

Sean Kelly via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 31 13:32:55 PDT 2014


On Thursday, 31 July 2014 at 19:28:24 UTC, Marc Schütz wrote:
> 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.

Huh.  For some reason I thought in-class initializations like
this were effectively rewritten to occur as a part of the class
ctor.  But looking at the docs I guess this actually affects the
format of the static initializer.


More information about the Digitalmars-d-learn mailing list