another compiler bug?
Aarti_pl
aarti at interia.pl
Fri Jan 11 06:35:28 PST 2008
Frits van Bommel pisze:
> Aarti_pl wrote:
>> torhu pisze:
>>> Aarti_pl wrote:
> [How to heap-allocate a class reference?]
>>
>> I managed to overcome bug in DMD with below:
>>
>> void func(T)() {
>> static TYPE res;
>> res = new T;
>> return &res;
>> }
>>
>> although it will cause memory leaks for every type for which the
>> function is instantiated. So I consider bug in DMD as quite serious...
>
> That will only allocate one reference per type (*not* per call to the
> function). Try this workaround instead:
That's exactly what I said :-)
> -----
> T* func(T)() {
> struct helper {
> T val;
> }
> return &(new helper).val;
> }
> -----
> This heap-allocates a struct containing only a reference and returns a
> pointer to that member.
Your solution probably works correctly, as another solution proposed by
torhu. Though I will stay with my workaround for now as probably due to
logic error in my app these solution also cause seg fault. But for now I
can not say precisely why...
>
> I don't think it's a bug in the compiler though, it behaves according to
> the spec. However, this seems to be an oversight in the spec as it
> doesn't provide for any way to directly allocate a "Class*".
Can you point me to correct place in spec?
IMHO it is a bug, as creating new class does not produce pointer but
reference. Well physically it is same thing but it should be obvious
what should be created:
new Class -- create reference to class -> physically *data
new Class* -- create pointer to reference -> physically **data
as you see in second example type is exactly same as compiler is
complaining about. Probably compiler should just figure out different
type from it: instead of Class** it should be just Class*.
BR
Marcin Kuszczak
(aarti_pl)
More information about the Digitalmars-d-learn
mailing list