printing error mesgs.

P

Pushkar Pradhan

I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, i.e. is there some variable or call
which knows where the last call failed?
Pushkar Pradhan
 
J

Jens.Toerring

Pushkar Pradhan said:
I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);

Better get rid of the cast - it won't help you and will only keep
the compiler from complaining if you forget to include said:
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, i.e. is there some variable or call
which knows where the last call failed?

No. The compiled soure doesn't know anything about line numbers,
__LINE__ is a preprocessor thingy, so the preprocessor will fill
in the line number before it's translated - if you look at the
assembler for the compiled program you probably will find that
__LINE__ has been replaced by a number. But even if you don't
subtract anything it will still be pretty obvious from the output
where the problem was, so why bother? That's for debugging, isn't
it, and not something you're going to ship to a customer?

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
M

Mike Wahler

Pushkar Pradhan said:
I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);

Obligatory admonition:

Don't cast the return value from 'malloc()'!

x = malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);

Note: -1 is not a portable argument for 'exit()'.
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, i.e. is there some variable or call
which knows where the last call failed?

I don't think so.

I normally try to avoid more that one statement per line,
but how about:

int line = 0;
float *x = NULL;

line = __LINE__; x = malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.\n", __FILE__, line);
exit(-1);
}


I don't know if you'd find it useful or not, but also note
the footnote to 6.10.8 / 1 :

(149) The presumed source file name and line number can
be changed by the #line directive.

-Mike
 
A

Alan Balmer

I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, i.e. is there some variable or call
which knows where the last call failed?
Pushkar Pradhan

I do this sort of thing regularly. Think about it - why do you care
whether the line number is that of the failing call, or two lines
after? When I see a message like that, I get the source in the editor,
go to the line specified, and it's then obvious which code is referred
to.

If you do this very much, you'll probably want to write an error
function which is passed __LINE__ and any other parameters of
interest, and formats the message. You can then write a macro which
includes the __LINE__ and __FILE__ without having to write them out
every time.
 
E

Eric Sosman

Pushkar said:
I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, i.e. is there some variable or call
which knows where the last call failed?

No, but printing plain `__LINE__' is usually good enough
to let you locate the failure.

Side-issue: `exit(-1)' is a poor idea, because different
systems interpret the `-1' value in different ways. (In fact,
I know of none that actually return a "negative one" as the
termination status.) `exit(EXIT_FAILURE)' is better.
 
P

Pushkar Pradhan

Ok with the line thing.
But my C book (kernighan richie) always cast the pointer to whatever typ
e we are allocating e.g.
int *ip;
ip = (int *)malloc(...)
They do the same for calloc.
Anyway I'm going to remove the CASTs.
 
R

Richard Heathfield

Pushkar said:
Ok with the line thing.
But my C book (kernighan richie) always cast the pointer to whatever typ
e we are allocating e.g.
int *ip;
ip = (int *)malloc(...)
They do the same for calloc.

The cast /used/ to be required, before the void * type was introduced. It is
no longer required. The book's errata Website reflects this fact.
Anyway I'm going to remove the CASTs.

Good plan.
 
M

Martin Ambuhl

Pushkar said:
I'm trying to print out error mesgs. in my program if some call fails.
I want to print the filename and the line no. in which it occurred.
For eg.
x = (float*)malloc(SIZE);
if(x == NULL) {
printf("%s %d: malloc failed.", __FILE__, __LINE__-2);
exit(-1);
}
However, this is a little unsophisticated as I've to calculate whether
it's 2 or 3 lines above the printf.
Is there a better way to do this, ...

To start with, you could use the defined argument EXIT_FAILURE rather than
one outside those exit() has standardly defined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top