Jump to page: 1 2
Thread overview
another compiler bug?
Jan 09, 2008
Aarti_pl
Jan 10, 2008
Robert DaSilva
Jan 10, 2008
Aarti_pl
Jan 10, 2008
torhu
Jan 10, 2008
Aarti_pl
Jan 10, 2008
Aarti_pl
Jan 10, 2008
Christopher Wright
Jan 10, 2008
Aarti_pl
Jan 11, 2008
Frits van Bommel
Jan 11, 2008
Aarti_pl
Jan 12, 2008
Frits van Bommel
Jan 12, 2008
Marcin Kuszczak
Jan 13, 2008
Frits van Bommel
Jan 13, 2008
Marcin Kuszczak
Jan 14, 2008
Frits van Bommel
January 09, 2008
How to create pointer to class?

Below doesn't compile:
----
A* cp = new A*;
----
Type of new A* is (A**) while it should be A*.

Any comments? (Maybe I missed something...)

PS. I know that it is possible to create pointer to class in two steps.

BR
Marcin Kuszczak
(aarti_pl)
January 10, 2008
Aarti_pl wrote:
> How to create pointer to class?
> 
> Below doesn't compile:
> ----
> A* cp = new A*;
> ----
> Type of new A* is (A**) while it should be A*.
> 
> Any comments? (Maybe I missed something...)
> 
> PS. I know that it is possible to create pointer to class in two steps.
> 
> BR
> Marcin Kuszczak
> (aarti_pl)

Because new returns a pointer to what you ask for. The reason new returns A when you ask it for A instead of *A like it would with anything else (anything not a class), is because A is actually a pointer to an instance of class A.

The true is this is probably the best behaver, maybe... This raise some important question, I think.
January 10, 2008
Aarti_pl wrote:
> How to create pointer to class?
> 
> Below doesn't compile:
> ----
> A* cp = new A*;
> ----
> Type of new A* is (A**) while it should be A*.
> 
> Any comments? (Maybe I missed something...)
> 
> PS. I know that it is possible to create pointer to class in two steps.


Do you actually want to heap allocate a pointer to an object reference?  Try this:

A* cp = *(new A*);

Maybe you really wanted to take the allocate an object, and then get the address of the reference?  I guess so, but I don't know if that's possible in a single statement.
January 10, 2008
torhu pisze:
> Aarti_pl wrote:
>> How to create pointer to class?
>>
>> Below doesn't compile:
>> ----
>> A* cp = new A*;
>> ----
>> Type of new A* is (A**) while it should be A*.
>>
>> Any comments? (Maybe I missed something...)
>>
>> PS. I know that it is possible to create pointer to class in two steps.
> 
> 
> Do you actually want to heap allocate a pointer to an object reference? 

Yes. After writing my post I discovered that 2 stage creation will not work for returning result from function. So my 'PS.' in fact is not solution.

> Try this:
> A* cp = *(new A*);

I needed something like above. Unfortunately your solution doesn't work (don't know why). I get access violation in my program when returning '*(new A*)'.

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...


> Maybe you really wanted to take the allocate an object, and then get the address of the reference?  I guess so, but I don't know if that's possible in a single statement.

No, it won't work in my program as I must return this pointer from function.

Thanks for your post!

BR
Marcin Kuszczak
(aarti_pl)
January 10, 2008
Robert DaSilva pisze:
> Aarti_pl wrote:
>> How to create pointer to class?
>>
>> Below doesn't compile:
>> ----
>> A* cp = new A*;
>> ----
>> Type of new A* is (A**) while it should be A*.
>>
>> Any comments? (Maybe I missed something...)
>>
>> PS. I know that it is possible to create pointer to class in two steps.
>>
>> BR
>> Marcin Kuszczak
>> (aarti_pl)
> 
> Because new returns a pointer to what you ask for. The reason new
> returns A when you ask it for A instead of *A like it would with
> anything else (anything not a class), is because A is actually a pointer
> to an instance of class A.
> 
> The true is this is probably the best behaver, maybe... This raise some
> important question, I think.

The problem here is that compiler does not return pointer to class reference as it should. That's why the line of code doesn't compile although it should.

I don't see why its not possible for compiler to behave correctly. Of course internally compiler should behave differently for classes than for other types. But this is just a consequence of decision that classes should be threated as value types in D.

BR
Marcin Kuszczak
(aarti_pl)
January 10, 2008
Aarti_pl pisze:
> torhu pisze:
>> Aarti_pl wrote:
>  > Try this:
>> A* cp = *(new A*);
> 
> I needed something like above. Unfortunately your solution doesn't work (don't know why). I get access violation in my program when returning '*(new A*)'.
> 
> 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...
> 

It can be of course also problem with logic in my program. I will check it.

BR
Marcin Kuszczak
(aarti_pl)
January 10, 2008
Aarti_pl wrote:
> torhu pisze:
>> Aarti_pl wrote:
>>> How to create pointer to class?
>>>
>>> Below doesn't compile:
>>> ----
>>> A* cp = new A*;
>>> ----
>>> Type of new A* is (A**) while it should be A*.
>>>
>>> Any comments? (Maybe I missed something...)
>>>
>>> PS. I know that it is possible to create pointer to class in two steps.
>>
>>
>> Do you actually want to heap allocate a pointer to an object reference? 
> 
> Yes. After writing my post I discovered that 2 stage creation will not work for returning result from function. So my 'PS.' in fact is not solution.
> 
>  > Try this:
>> A* cp = *(new A*);
> 
> I needed something like above. Unfortunately your solution doesn't work (don't know why). I get access violation in my program when returning '*(new A*)'.
> 
> 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...

Does that work? With more than one instance created? I would think you'd only ever be able to access the most recent one.
January 10, 2008
Christopher Wright pisze:
>> 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...
> 
> Does that work? With more than one instance created? I would think you'd only ever be able to access the most recent one.

Yes. I used something similar in C++.

BR
Marcin Kuszczak
(aarti_pl)
January 11, 2008
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:
-----
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.


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*".
January 11, 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)
« First   ‹ Prev
1 2