RAII who?
Lutger
lutger.blijdestijn at gmail.com
Sat May 19 04:46:39 PDT 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!
> Jan
According 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.
More information about the Digitalmars-d
mailing list