C
Carlos
I have a macro
#define DIE(msg) do { fprintf(stderr, "%s (l.%d)\n", msg, __LINE__);\
exit(1); }\
while (0)
and it works . But later I thought, that if I use it like this:
s = malloc(2000); if (!s) DIE("malloc failed!");
it is possible that the fprintf will fail too, trying to allocate memory
to build the output string (or probably not, I don't know the internals;
and also, I think in Linux mallocs never fail, but anyway).
So I thought of using a "simpler" function to display the message,
something like
fputs(msg "\n", stderr)
But how can I add __LINE__?
fputs(msg #__LINE__ "\n", stderr)
doesn't work (the compiler says that "'#' is not followed by a macro
parameter").
Changing DIE do
#define DIE(msg, line) fputs(msg #lines, stderr)
and calling it this way: 'DIE("error", __LINE__);' results in
'fputs("error" "__LINE__", stderr)'.
I want of course 'fputs("error" "145", stderr)' or something like that.
Any ideas?
#define DIE(msg) do { fprintf(stderr, "%s (l.%d)\n", msg, __LINE__);\
exit(1); }\
while (0)
and it works . But later I thought, that if I use it like this:
s = malloc(2000); if (!s) DIE("malloc failed!");
it is possible that the fprintf will fail too, trying to allocate memory
to build the output string (or probably not, I don't know the internals;
and also, I think in Linux mallocs never fail, but anyway).
So I thought of using a "simpler" function to display the message,
something like
fputs(msg "\n", stderr)
But how can I add __LINE__?
fputs(msg #__LINE__ "\n", stderr)
doesn't work (the compiler says that "'#' is not followed by a macro
parameter").
Changing DIE do
#define DIE(msg, line) fputs(msg #lines, stderr)
and calling it this way: 'DIE("error", __LINE__);' results in
'fputs("error" "__LINE__", stderr)'.
I want of course 'fputs("error" "145", stderr)' or something like that.
Any ideas?