On Oct 27, 9:06*am, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> ImpalerCore <jadil...@gmail.com> wrote innews:3e400603-4923-4655-b4b2-:
> > On Oct 26, 7:42 pm, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> >> ImpalerCore <jadil...@gmail.com> wrote in
> >> news:fb6dea90-99b9-4ac5-88b0-
> >> 24aafbea2...@ez26g2000vbb.googlegroups.com:
>
> >> > One option to do RAII in C in a limited sense is to reserve a
> >> > buffer of automatic storage within a function, and then direct
> >> > allocations to use that buffer.
>
> >> This again assumes RAII is only about releasing memory. If this was
> >> the case, one could just add a Boehm garbage collector to the program
> >> and forget about all those issues. In reality, RAII in C++ is
> >> equivalently important for mutex unlocking, file handle closing,
> >> ending the sandclock mode of the mouse cursor, etc, etc.
>
> > Originally RAII was designed to handle memory cleanup in the presence
> > of exceptions, as it's pretty much a necessity to write exception safe
> > code in C++.
>
> Just curious, do you have any links to support this? Exception safe code
> needs to clean up all resources anyway, not only memory. My google-fu
> only produced wordings containing the general term "resources", including
> Stroustrup himself (http://www.velocityreviews.com/forums/t688168-who-
> invented-deterministic-construction-destruction.html).
No, just my personal exposure to it in the context of my old C++
college courses, which was explained for the purpose of deallocating
memory. I'm sure Bjarne designed it for files and other more exotic
constructs.
> > I would say that when people saw how good RAII was in
> > memory cleanup that it was applied to other resource cleanup as well.
> > Hence the phrase "limited sense", as C does not provide language
> > support of anything resembling destructors.
>
> BTW, I just noticed a gcc extension doing something almost exactly like
> this (a "cleanup" attribute):http://en.wikipedia.org/wiki/Resourc...itialization#G...
> xtensions_for_C
Sure, if you restrict your code to a compiler toolchain that supports
that extension. I have a habit of avoiding 'gcc-isms' or other
compiler-isms for library development when I can.
> >> For stack allocations there is the alloca() function, and also C99
> >> VLA-s, no need to reinvent the wheel. Or is this something different
> >> you are talking about?
>
> >> A general problem with using variable amounts of stack memory is that
> >> the total amount of the stack space is quite small and
> >> implementation- dependent and a stack overflow yields UB without any
> >> standard detection or interception means. So writing a robust program
> >> with variable-size stack allocations is pretty hard.
>
> > The difference is that this mechanism is a "fixed" stack allocation.
> > One issue is that alloca is not standard, and C99 VLAs do not apply to
> > C90 environments, which limit the scope of environments they can be
> > applied. *While the 'region' technique has the same recursion issues
> > that alloca and C99 VLAs have, the stack size reserved to an algorithm
> > that needs to allocate memory is fixed. *That means that passing big
> > input doesn't result in a big alloca or VLA resize that blows up the
> > stack.
>
> [snipped lengthy example code of fixed-pool-size stack allocator scheme]
>
> > The key point to take away from this example is that the size of the
> > stack used is independent of the size of s1 and s2. *While alloca and
> > VLAs are convenient, they lend themself to a programming style where
> > it's easy to blow the stack in the presence of a big string.
>
> OK, I can see now how this scheme can be indeed useful for more reliable
> stack-based allocations. In this sense it reminds me the short string
> optimization techniques used by some C++ implementations. There, if the
> string is shorter than a given threshold, it is stored directly inside
> the string object (which is often a local variable on stack). Only larger
> strings require a dynamic memory allocation. This mechanism is fully
> encapsulated of course, as is customary in C++, so that the class users
> do not have to know or care about such implementation details.
Agreed, but I also think the "RAII for memory" is also encapsulated in
'c_levenshtein', unless I misunderstand what you're saying by
"encapsulation". By that I mean that the c_levenshtein just takes two
strings; there's no c_region being passed as a parameter and the user
can call it whether 'c_levenshtein' used the region or just plain
malloc.
Best regards,
John D.