digitalmars.D - RAII who?
- Jan Hanselaer (16/16) May 19 2007 Hi
- Daniel Keep (38/58) May 19 2007 Nothing to do with it. Initialisation to a default value is just
- Lutger (16/36) May 19 2007 According to wikipedia: "The acquisition is bound to the construction
- Lutger (2/4) May 19 2007 Forget to mention, this is in C++ not in D.
- Jan Hanselaer (14/52) May 19 2007 Thanks for the explanation Daniel en Lutger.
- Lutger (18/34) May 19 2007 I think the main issue is lifetime. A garbage collector can decide to
- Lutger (3/3) May 19 2007 Also, in the face of possible exceptions, it is much cleaner to use
- Walter Bright (3/4) May 19 2007 Linky:
Hi I'm confused about what's meant with Resource Acquisition Is Initialisation. Can somebody explain this, especially how it is accomplished in D? Some questions that arise in my brain when reading and thinking about it. - Giving each variable a default value is this part of RAII? Or has it nothing to do with it? - What's the use of the keyword 'auto' in the whole story? - What's the link with garbage collection? - If I don't use the keyword auto, memory resources are not automatically freed? - ... I just don't seem to get te whole picture here. But I suspect it's not that complicated. Anyone with a short and clear definition? Thanks in advance! Jan
May 19 2007
Jan Hanselaer wrote:Hi I'm confused about what's meant with Resource Acquisition Is Initialisation. Can somebody explain this, especially how it is accomplished in D? Some questions that arise in my brain when reading and thinking about it. - Giving each variable a default value is this part of RAII? Or has it nothing to do with it?Nothing to do with it. Initialisation to a default value is just something to help catch bugs. For integer types, this doesn't really do much, but floating-point, character and reference types, they all get "invalid" values which make it pretty obvious when you've forgotten to initialise something.- What's the use of the keyword 'auto' in the whole story?It isn't. It *used* to be the way to trigger RAII, but not any more. Now you want the 'scope' keyword.- What's the link with garbage collection?RAII instances of classes do not need to be garbage collected; they are deterministically destroyed. See below.- If I don't use the keyword auto, memory resources are not automatically freed?Here's a "normal" class instance declaration: auto Foo foo = new Foo; Here, "foo" can either be explicitly destroyed using 'delete', or it will be garbage collected when nothing refers to it. On the other hand: scope Foo foo = new Foo; What happens here is that "foo" is now scoped. At the end of the declaring scope (which could be the function, conditional branch, current iteration of a loop, etc.), the object will be destroyed and freed. Please note that, AFAIK, scope doesn't work on any other reference type: arrays, pointers or associative arrays. Also note that the above are also equivalent to: "auto Foo foo = new Foo;" == "Foo foo = new Foo;" == "auto foo = new Foo;" "scope Foo foo = new Foo;" == "scope foo = new Foo;"- ... I just don't seem to get te whole picture here. But I suspect it's not that complicated. Anyone with a short and clear definition? Thanks in advance! JanI don't really think of it as "RAII" anymore: I think of it as "scoped objects" -- objects which live only as long as the current scope. -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 19 2007
Jan Hanselaer wrote:Hi I'm confused about what's meant with Resource Acquisition Is Initialisation. Can somebody explain this, especially how it is accomplished in D? Some questions that arise in my brain when reading and thinking about it. - Giving each variable a default value is this part of RAII? Or has it nothing to do with it? - What's the use of the keyword 'auto' in the whole story? - What's the link with garbage collection? - If I don't use the keyword auto, memory resources are not automatically freed? - ... I just don't seem to get te whole picture here. But I suspect it's not that complicated. Anyone with a short and clear definition? Thanks in advance! JanAccording to wikipedia: "The acquisition is bound to the construction (initialization) whereas the release is bound to the automatic, deterministic destruction (uninitialization) of the variable. Since a destructor of an automatic variable is called when leaving its scope, it can be guaranteed that the resource is released as soon as the variable's life time ends." Thus, if we are talking about memory as a resource, this usually means for every new in the constructor a delete in the destructor. The link with garbage collection is that it is an alternative technique often used in C++, to make it easier and safer to manage memory. Less so in D, since there is the garbage collector. Auto used to be what scope classes are now. In D, contrary to C++, destructors are not automatically called upon scope exit. With scope classses they are, so these are more useful for RAII, for example file handles.
May 19 2007
Lutger wrote:Thus, if we are talking about memory as a resource, this usually means for every new in the constructor a delete in the destructor.Forget to mention, this is in C++ not in D.
May 19 2007
Thanks for the explanation Daniel en Lutger. Some new qestions come to my mind. Since now there is the 'scope' attribute, the auto attribute only exists for type inference? I have a background in Java and consequently not used to work with destructors. But in D apparently it is used although there is a garbage collector. Are there any (style) rules that say when the scope attribute (and a destructor) is needed? I would expect the garbage collector cleaning up all the resources when an object gets out of scope ... So till now I never used a destructor and/or the scope attribute. Jan "Lutger" <lutger.blijdestijn gmail.com> schreef in bericht news:f2mo2h$22e$1 digitalmars.com...Jan Hanselaer wrote:Hi I'm confused about what's meant with Resource Acquisition Is Initialisation. Can somebody explain this, especially how it is accomplished in D? Some questions that arise in my brain when reading and thinking about it. - Giving each variable a default value is this part of RAII? Or has it nothing to do with it? - What's the use of the keyword 'auto' in the whole story? - What's the link with garbage collection? - If I don't use the keyword auto, memory resources are not automatically freed? - ... I just don't seem to get te whole picture here. But I suspect it's not that complicated. Anyone with a short and clear definition? Thanks in advance! JanAccording to wikipedia: "The acquisition is bound to the construction (initialization) whereas the release is bound to the automatic, deterministic destruction (uninitialization) of the variable. Since a destructor of an automatic variable is called when leaving its scope, it can be guaranteed that the resource is released as soon as the variable's life time ends." Thus, if we are talking about memory as a resource, this usually means for every new in the constructor a delete in the destructor. The link with garbage collection is that it is an alternative technique often used in C++, to make it easier and safer to manage memory. Less so in D, since there is the garbage collector. Auto used to be what scope classes are now. In D, contrary to C++, destructors are not automatically called upon scope exit. With scope classses they are, so these are more useful for RAII, for example file handles.
May 19 2007
Jan Hanselaer wrote:Thanks for the explanation Daniel en Lutger. Some new qestions come to my mind. Since now there is the 'scope' attribute, the auto attribute only exists for type inference? I have a background in Java and consequently not used to work with destructors. But in D apparently it is used although there is a garbage collector. Are there any (style) rules that say when the scope attribute (and a destructor) is needed? I would expect the garbage collector cleaning up all the resources when an object gets out of scope ... So till now I never used a destructor and/or the scope attribute. JanI think the main issue is lifetime. A garbage collector can decide to not collect your unreferenced objects because there is no need for it. Furthermore, the collector will only run on calls to new (or if you poke it), so objects will usually persist beyond their scope. Thus your expectation that the garbage collector cleans all resources when objects go out of scope does not always hold, it cleans them up...eventually (which can be program exit) The main rule I follow is to ask myself if it's better to tie some resource to a scope. Think about file handles. You want to open the file, read it, and then unlock the handle ASAP. This fits very nicely into a scope, either with a scope class FileHandle or scope(exit) closeFile(). I think the need for RAII objects in D (and destructors in this context) is not so pervasive as in C++, simply because the most prevalent resource - memory - is usually handled by the garbage collector. Then there is scope(exit/succes/failure) which is very useful too and can do what scope classes are sometimes used for more directly.
May 19 2007
Also, in the face of possible exceptions, it is much cleaner to use scope than try..catch..finally: http://www.digitalmars.com/d/exception-safe.html
May 19 2007
Lutger wrote:According to wikipedia:Linky: http://en.wikipedia.org/wiki/RAII
May 19 2007