RAII in C
RAII in C
Posted Mar 5, 2014 16:53 UTC (Wed) by cesarb (subscriber, #6266)In reply to: A longstanding GnuTLS certificate validation botch by zorro
Parent article: A longstanding GnuTLS certificate validation botch
Posted Mar 5, 2014 17:03 UTC (Wed)
by tjc (guest, #137)
[Link] (6 responses)
Posted Mar 5, 2014 17:36 UTC (Wed)
by bronson (subscriber, #4806)
[Link] (5 responses)
But if there's a better way I really want to hear it!
Posted Mar 5, 2014 20:29 UTC (Wed)
by mathstuf (subscriber, #69389)
[Link]
Posted Mar 5, 2014 21:29 UTC (Wed)
by cesarb (subscriber, #6266)
[Link] (2 responses)
systemd's way is: Using the following definitions: (Actually, fclosep is defined via another macro, DEFINE_TRIVIAL_CLEANUP_FUNC, but it expands to the static inline I typed above.) Not that ugly (compare "_cleanup_fclose_ FILE *f = NULL;" with a C++ equivalent of "scoped_FILE f(nullptr);", assuming an equivalent scoped_FILE helper class). Yes, C++'s way is slightly cleaner to use (and way more complex to define), but the difference is not that big.
Posted Mar 6, 2014 18:43 UTC (Thu)
by bronson (subscriber, #4806)
[Link] (1 responses)
Looking forward to playing with these macros next time I'm waist deep in C.
Posted Mar 6, 2014 22:52 UTC (Thu)
by cesarb (subscriber, #6266)
[Link]
Posted Apr 9, 2014 13:02 UTC (Wed)
by psevon (guest, #96490)
[Link]
RAII in C
Yeah! If they're talking about this:
RAII in C
void cleanup_file(FILE **fp)
{
// handle a bunch of boundary conditions, probably using global variables
...
}
int main() {
FILE *fp __attribute__ ((__cleanup__(cleanup_file)));
...
}
then ew.
RAII in C
RAII in C
void foo(void) {
_cleanup_fclose_ FILE *f = NULL;
...
}
#define _cleanup_(x) __attribute__((cleanup(x)))
static inline void fclosep(FILE **p) {
if (*p)
fclose(*p);
}
#define _cleanup_fclose_ _cleanup_(fclosep)
RAII in C
RAII in C
RAII in C
