digitalmars.D.learn - another compiler bug?
- Aarti_pl (11/11) Jan 09 2008 How to create pointer to class?
- Robert DaSilva (7/22) Jan 09 2008 Because new returns a pointer to what you ask for. The reason new
- Aarti_pl (11/35) Jan 10 2008 The problem here is that compiler does not return pointer to class
- torhu (7/18) Jan 09 2008 Do you actually want to heap allocate a pointer to an object reference?
- Aarti_pl (20/40) Jan 10 2008 Yes. After writing my post I discovered that 2 stage creation will not
- Aarti_pl (5/25) Jan 10 2008 It can be of course also problem with logic in my program. I will check ...
- Christopher Wright (3/41) Jan 10 2008 Does that work? With more than one instance created? I would think you'd...
- Aarti_pl (5/18) Jan 10 2008 Yes. I used something similar in C++.
- Frits van Bommel (17/29) Jan 11 2008 That will only allocate one reference per type (*not* per call to the
- Aarti_pl (18/50) Jan 11 2008 Your solution probably works correctly, as another solution proposed by
- Frits van Bommel (6/27) Jan 12 2008 No, you say it will allocate a reference per instantiation and refer to
- Marcin Kuszczak (41/54) Jan 12 2008 That leads me to discuss another bug which I discovered.
- Frits van Bommel (21/60) Jan 13 2008 No, it's just initialized once. However, the struct itself isn't
- Marcin Kuszczak (11/42) Jan 13 2008 Some of them probably are bugs, and some of them are not. That's why I
- Frits van Bommel (4/6) Jan 14 2008 I'd say if you're not sure if they're bugs, best post here first. Then
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)
Jan 09 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.
Jan 09 2008
Robert DaSilva pisze:Aarti_pl wrote: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)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.
Jan 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.
Jan 09 2008
torhu pisze:Aarti_pl wrote: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.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*);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)
Jan 10 2008
Aarti_pl pisze:torhu pisze:It can be of course also problem with logic in my program. I will check it. BR Marcin Kuszczak (aarti_pl)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...
Jan 10 2008
Aarti_pl wrote:torhu pisze:Does that work? With more than one instance created? I would think you'd only ever be able to access the most recent one.Aarti_pl wrote: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: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?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...
Jan 10 2008
Christopher Wright pisze:Yes. I used something similar in C++. BR Marcin Kuszczak (aarti_pl)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.
Jan 10 2008
Aarti_pl wrote:torhu pisze:[How to heap-allocate a class reference?]Aarti_pl wrote: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*".
Jan 11 2008
Frits van Bommel pisze:Aarti_pl wrote:That's exactly what I said :-)torhu pisze:[How to heap-allocate a class reference?]Aarti_pl wrote: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.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)
Jan 11 2008
Aarti_pl wrote:Frits van Bommel pisze:No, you say it will allocate a reference per instantiation and refer to this as a memory leak (presumably because they're not gc-ed after they're no longer in use). My point was that a second call to the function (for the same type) will overwrite the location returned from the first call.Aarti_pl wrote:That's exactly what I said :-)torhu pisze:[How to heap-allocate a class reference?]Aarti_pl wrote: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:
Jan 12 2008
Frits van Bommel wrote:Yes, I probably should say "memory leak" (singular) to express what I meant.No, you say it will allocate a reference per instantiation and refer to this as a memory leak (presumably because they're not gc-ed after they're no longer in use).That's exactly what I said :-)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:My point was that a second call to the function (for the same type) will overwrite the location returned from the first call.That leads me to discuss another bug which I discovered. Let's say we have program like below: -------- import std.stdio; struct Pattern(T : bool) { string beg = "test"; } Pattern!(T) get(T)() { static Pattern!(T) pattern; return pattern; } void main() { writefln(get!(bool).beg); get!(bool).beg = "other"; writefln(get!(bool).beg); } -------- Above program prints: test test although I would expect it will print: test other It seems that memory reserved for struct in template function is initialized every time the function is executed. This behaviour is different when we change Pattern to be class instead of struct. Then it is possible to create kind of singleton pattern for every type. What do you think? BTW. Where should I discuss bugs? I have another dozen of them and would like to discuss some of them before posting to bugzilla. Is it better to post to d.learn or d.bugs? -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus - http://www.zapytajmnie.com (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Jan 12 2008
Marcin Kuszczak wrote:That leads me to discuss another bug which I discovered. Let's say we have program like below: -------- import std.stdio; struct Pattern(T : bool) { string beg = "test"; } Pattern!(T) get(T)() { static Pattern!(T) pattern; return pattern; } void main() { writefln(get!(bool).beg); get!(bool).beg = "other"; writefln(get!(bool).beg); } -------- Above program prints: test test although I would expect it will print: test other It seems that memory reserved for struct in template function is initialized every time the function is executed.No, it's just initialized once. However, the struct itself isn't returned; a *copy* of it is returned. That copy is what's modified by the assignment. The second call will return a fresh copy of the original (with "test" remaining the string value).This behaviour is different when we change Pattern to be class instead of struct. Then it is possible to create kind of singleton pattern for every type.Yes, class instances are passed by reference, so the assignment would change the 'static' instance (assuming you 'new' it the first time around, so there is one). The same effect should be observed if you change the function to: ----- Pattern!(T)* get(T)() { static Pattern!(T) pattern; return &pattern; } ----- for the struct version (returning a pointer to the global instance instead of a copy of it).What do you think? BTW. Where should I discuss bugs? I have another dozen of them and would like to discuss some of them before posting to bugzilla. Is it better to post to d.learn or d.bugs?If they are of the same kind as this one, you may want to stick to d.learn as it's not actually a bug :P. (Or at least, it's a bug in your code rather than in the compiler; the compiler does exactly what I'd expect it to do and what IMO it should do)
Jan 13 2008
Frits van Bommel wrote:No, it's just initialized once. However, the struct itself isn't returned; a *copy* of it is returned. That copy is what's modified by the assignment. The second call will return a fresh copy of the original (with "test" remaining the string value).Thanks! Now this behaviour is clear for me.This behaviour is different when we change Pattern to be class instead of struct. Then it is possible to create kind of singleton pattern for every type.Yes, class instances are passed by reference, so the assignment would change the 'static' instance (assuming you 'new' it the first time around, so there is one). The same effect should be observed if you change the function to: ----- Pattern!(T)* get(T)() { static Pattern!(T) pattern; return &pattern; } ----- for the struct version (returning a pointer to the global instance instead of a copy of it).Some of them probably are bugs, and some of them are not. That's why I prefer to discuss them before posting to bugzilla. -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus - http://www.zapytajmnie.com (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------BTW. Where should I discuss bugs? I have another dozen of them and would like to discuss some of them before posting to bugzilla. Is it better to post to d.learn or d.bugs?If they are of the same kind as this one, you may want to stick to d.learn as it's not actually a bug :P. (Or at least, it's a bug in your code rather than in the compiler; the compiler does exactly what I'd expect it to do and what IMO it should do)
Jan 13 2008
Marcin Kuszczak wrote:Some of them probably are bugs, and some of them are not. That's why I prefer to discuss them before posting to bugzilla.I'd say if you're not sure if they're bugs, best post here first. Then once you're sure something is a bug you can post it to bugzilla. This way, D.bugs doesn't get cluttered with non-bugs.
Jan 14 2008