Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 25, 2003 HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
The small piece of code, just a class named Vector and method set to set its x,y and z: class Vector { public: real a,b,c; this(real aa, real bb, real cc){set(aa,bb,cc);} this(){set(0,0,0);} void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} } And now: Vector a; a.set(3,4,5); gives access violation. Newing 'a' helps though. What am I doing wrong? |
August 25, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to QUS | QUS wrote: > > The small piece of code, just a class named Vector and method set to set its x,y and z: > > class Vector > { > public: > real a,b,c; > this(real aa, real bb, real cc){set(aa,bb,cc);} > this(){set(0,0,0);} > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} > } > > And now: > > Vector a; > a.set(3,4,5); > > gives access violation. Newing 'a' helps though. What am I doing wrong? Nothing. You have to use "new". "Object o;" doesn't create an Object, but only a variable to hold a reference to an Object. It's like a pointer in C. -- Helmut Leitner leitner@hls.via.at Graz, Austria www.hls-software.com |
August 25, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to QUS | In article <bid1fk$nar$1@digitaldaemon.com>, QUS says... > >The small piece of code, just a class named Vector and method set to set its x,y and z: > >class Vector >{ > public: > real a,b,c; > this(real aa, real bb, real cc){set(aa,bb,cc);} > this(){set(0,0,0);} > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} >} > >And now: > >Vector a; >a.set(3,4,5); > >gives access violation. Newing 'a' helps though. What am I doing wrong? Nothing, all objects in D are allocated on the heap. For my own vector class I used a struct which can be allocated on the stack. |
August 25, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Helmut Leitner | > > The small piece of code, just a class named Vector and method set to set its > > x,y and z: > > > > class Vector > > { > > public: > > real a,b,c; > > this(real aa, real bb, real cc){set(aa,bb,cc);} > > this(){set(0,0,0);} > > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} > > } > > > > And now: > > > > Vector a; > > a.set(3,4,5); > > > > gives access violation. Newing 'a' helps though. What am I doing wrong? > > Nothing. You have to use "new". > > "Object o;" doesn't create an Object, but only a variable to hold a reference to an Object. It's like a pointer in C. > Then in case like that the compiler should produce an error as we are calling a member of an object that was not created or at least give a warning... IMO, we should not allows to call non-static member function on an object that is known to be null... Also speaking of that, we should have a way to indicate weither parameters can be null (I would vote for no by default). class A { } void f(in A a) { ... } A a; f(a); // should it be allowed ? We should have a modifier for that... and the compiler would be able to do compile and run-time checks as appropriate. It could maybe be usefull to have modifier or attributes to control object creation... - can it be created only on stack (auto modifier), only with new (no modifier) or both... or with alloca... or placement... - does we allows the declaration of null object or the object must be newed... |
August 26, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "Patrick Down" <Patrick_member@pathlink.com> wrote in message news:bid9ai$13eo$1@digitaldaemon.com... > In article <bid1fk$nar$1@digitaldaemon.com>, QUS says... > > > >The small piece of code, just a class named Vector and method set to set its > >x,y and z: > > > >class Vector > >{ > > public: > > real a,b,c; > > this(real aa, real bb, real cc){set(aa,bb,cc);} > > this(){set(0,0,0);} > > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} > >} > > > >And now: > > > >Vector a; > >a.set(3,4,5); > > > >gives access violation. Newing 'a' helps though. What am I doing wrong? > > Nothing, all objects in D are allocated on the heap. For my own vector class I used a struct which can be allocated on the stack. > you can use auto Vector a; it should be created for you (not on the stack, but will be destroyed when it goes out of scope). |
August 26, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wynn | > you can use
> auto Vector a;
> it should be created for you (not on the stack, but will be destroyed when
> it goes out of scope).
Is it legal to create an auto instance of a class that is not auto?
I mean I think it's a good thing, just not aware of rule.
|
August 26, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson | In article <bigl88$6gl$1@digitaldaemon.com>, Matthew Wilson says... > >> you can use >> auto Vector a; >> it should be created for you (not on the stack, but will be destroyed when >> it goes out of scope). > >Is it legal to create an auto instance of a class that is not auto?\ Yes > >I mean I think it's a good thing, just not aware of rule. > |
August 27, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "Patrick Down" <Patrick_member@pathlink.com> wrote in message news:bignu2$ahb$1@digitaldaemon.com... > In article <bigl88$6gl$1@digitaldaemon.com>, Matthew Wilson says... > > > >> you can use > >> auto Vector a; > >> it should be created for you (not on the stack, but will be destroyed when > >> it goes out of scope). > > > >Is it legal to create an auto instance of a class that is not auto?\ > > Yes > Well I guess I can imagine rationale for that design, but still it seems a pain. :( |
August 27, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | > In article <bid1fk$nar$1@digitaldaemon.com>, QUS says... > > > >The small piece of code, just a class named Vector and method set to set its > >x,y and z: > > > >class Vector > >{ > > public: > > real a,b,c; > > this(real aa, real bb, real cc){set(aa,bb,cc);} > > this(){set(0,0,0);} > > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} > >} > > > >And now: > > > >Vector a; > >a.set(3,4,5); > > > >gives access violation. Newing 'a' helps though. What am I doing wrong? > > Nothing, all objects in D are allocated on the heap. For my own vector class I used a struct which can be allocated on the stack. > Hmm... I just wonder... How would it work performance-wise in a game? I guess each operator overloading function also has to new its return value? Now what if I have a solar system with (currently!) over 100 bodies, for which I have to calculate postions 50 times a second? :-) |
October 23, 2003 Re: HOW can I make a local stack variable?! | ||||
---|---|---|---|---|
| ||||
Posted in reply to QUS | "QUS" <qus@go2.pl> wrote in news:bihnmc$1tg1$1@digitaldaemon.com: >> In article <bid1fk$nar$1@digitaldaemon.com>, QUS says... >> > >> >The small piece of code, just a class named Vector and method set to set > its >> >x,y and z: >> > >> >class Vector >> >{ >> > public: >> > real a,b,c; >> > this(real aa, real bb, real cc){set(aa,bb,cc);} >> > this(){set(0,0,0);} >> > void set(real aa, real bb, real cc){a=aa; b=bb; c=cc;} >> >} >> > >> >And now: >> > >> >Vector a; >> >a.set(3,4,5); >> > >> >gives access violation. Newing 'a' helps though. What am I doing wrong? >> >> Nothing, all objects in D are allocated on the heap. For my own vector class I used a struct which can be allocated on the stack. >> > Hmm... I just wonder... How would it work performance-wise in a game? I guess each operator overloading function also has to new its return value? Now what if I have a solar system with (currently!) over 100 bodies, for which I have to calculate postions 50 times a second? :-) I'm actually wondering the same thing. I did a little test where I had a vector like object being created every frame and the memory usage did seem to fluctuate heavily, though the test wasn't extensive enough to decide if performance was influenced. Creating the object once and reusing it resulted in a nice steady memory usage. Perhaps anyone else has more experience with the performance issues. |
Copyright © 1999-2021 by the D Language Foundation