Debugging, statics.

Piercarlo Grandi pcg at aber-cs.UUCP
Fri Dec 23 02:31:52 AEST 1988


In article <127 at mole-end.UUCP> mat at mole-end.UUCP (Mark A Terribile) writes:

#	[ ... where I (pcg) point out that if one implements generators state
#	in explicit structs one can build multiple instances of them ... ]

#    True, but please allow me to point out that this problem is trivial in
#    C++ (I know, this is a group about C) and that using a struct to
#    represent the state is exactly the *implementation* of the C++ idiom.

Indeed, indeed. Hail C++! It saves drudgery. By the way, Stroustrup has
indeed written a fairly general coroutines/generators package, and he does
demonstrate writing "iterator" classes in his book. Others have used them,
too. On my little own, I have written a generalized package that with a
little syntax macros does that in C as well, and I am going to convert it to
a C++ version as soon as I can.

    The difficulty in C is that you can't force an instance of a struct to be
    initialized when it is declared, especially if it is declared locally.
    Because there is no automatic aggregate initialization, you end up
    resorting to statics.

Agreed, but there is a also a question of a bit of laziness. Almost every
time I declare a struct in C I define create and delete procedures for it, as
a matter of habit, but I do too occasionally give in to the laziness of not
struct'ifying the state of an impure function, and then as often I get
bitten.

Clearly the fact that C++ invites and helps you to declare constructors and
destructors does help a lot in overcoming laziness.  They are also useful in
other ways; I have recently (re)posted to comp.lang.c++ a somewhat neat way
of using them to implement shallow cound, dynamically scoped variables (e.g.
exception handlers).

Let me add that C++ classes, as helpful as they are to hold state, are only
half of the full solution: to have a truly generalized generator coroutine
facility, you also need non local control transfers, to implement suspend and
resume.  I used a little known "feature" of PCC to implement this (taking &
of a label...), but clearly there ought to be a language supported mechanism.
This is a very general problem that algorithmic languages do not support very
well, save for noble exceptions (e.g. SL5/Icon).

Stroustrup (and Tiemann) are supposed to be wroking on neater ways to
implement non nested CONTROL transfer, just as C++ classes can be used
implement non nested environements.

    I have no good solution except to take the plunge UPWARD to C++.

Highly recommended, I have already done that, with special thanks to g++.
-- 
Piercarlo "Peter" Grandi			INET: pcg at cs.aber.ac.uk
Sw.Eng. Group, Dept. of Computer Science	UUCP: ...!mcvax!ukc!aber-cs!pcg
UCW, Penglais, Aberystwyth, WALES SY23 3BZ (UK)



More information about the Comp.lang.c mailing list