By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

printing error mesgs.

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Pushkar Pradhan <pu*****@gri.msstate.edu> wrote:
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 <stdlib.h>.
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2

P: n/a

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
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
Nov 13 '05 #3

P: n/a
On Fri, 21 Nov 2003 16:05:38 -0600, Pushkar Pradhan
<pu*****@gri.msstate.edu> wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #4

P: n/a
Pushkar Pradhan wrote:

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.

--
Er*********@sun.com
Nov 13 '05 #5

P: n/a
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.
Mike Wahler wrote:
"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
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


Nov 13 '05 #6

P: n/a
Pushkar Pradhan wrote:
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #7

P: n/a
Pushkar Pradhan wrote:
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.

--
Martin Ambuhl

Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.