ponce

Jonathan M Davis jmdavisProg at gmx.com
Wed Nov 10 10:08:10 PST 2010


On Wednesday, November 10, 2010 07:55:42 Steven Schveighoffer wrote:
> On Tue, 09 Nov 2010 17:14:33 -0500, Don <nospam at nospam.com> wrote:
> > bearophile wrote:
> >> Jonathan M Davis:
> >>> it would be possible to make it so that any objects allocated with new
> >>> during CTFE would be in the dynamic heap during runtime.
> >>> 
> >>  This is possible, but it doesn't seem what you usually desire when you
> >> 
> >> allocate an object at compile time.
> >> 
> >>  Bye,
> >> 
> >> bearophile
> > 
> > If it's mutable, it'll go on the heap. If it's immutable, it could
> > optionally go into read-only memory (it will be exactly like the .init
> > of a class instance). Classes which are used only during execution of
> > CTFE functions will not be instantiated at runtime.
> 
> Pardon my ignorance, but how can something evaluated at compile time go on
> the heap?  The heap doesn't exist yet!
> 
> e.g.:
> 
> class C
> {
>    int[] buffer;
>    this() pure { buffer = new int[125];}
> }
> 
> C c = new C;
> 
> How does c go on the heap at compile time?  Won't you have to re-run the
> constructor at runtime to get the right result?  Not only that, but even
> if you did run the ctor at compile time, how do you make a copy of c for
> every thread without re-running the ctor?

You likely end up essentially serializing it. You then deserialized it and bring 
it into the heap when the program loads. It's not a particularly pretty problem, 
but it should be quite doable. You certainly wouldn't rerun the constructor or 
whatnot. What you need is to just transfer its saved state onto the heap, 
translating any pointers and references that it has to whatever they should be 
in the current heap.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list