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 7 2816
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
"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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: alb120 |
last post by:
I implementing Internet Printing; but it fails:
http://w23ksv/printers/
gives error on EI Version 6.0.2800.1106 (XP)
"Server object error 'ASP 0193 : 80020009'
OnStartPage Failed...
|
by: hamil |
last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview
control, the PageSetup control, and the Print dialog control. The code
attached is a concatination of two examples taken out...
|
by: John Peterson |
last post by:
Hello all!
I'm at my wits end trying to search for what I assumed to be a relatively
straightforward task. I have a Web application written in C#, and I have a
button on the form that I want to...
|
by: eskildb |
last post by:
First, please be gently. I am fairly new to the programming world (1.5
years with some expermentation prior to).
I have been working on a project that has to print HTML pages with
graphics in a...
|
by: eskildb |
last post by:
First, please be gently. I am fairly new to the programming world (1.5
years with some expermentation prior to).
I have been working on a project that has to print HTML pages with
graphics in a...
|
by: Rick Lederman |
last post by:
I am using a PrintDocument and PrintDialog to print. The first time that I
print it works, but when I try to print a second time without exiting the
entire program I get an...
|
by: Peter |
last post by:
ASP.NET 3.5
I am trying to print from a Webservice on Windows 2003 box, but I am getting
the following error when I try to print
I have tried the following solution but it did not help...
|
by: Brett |
last post by:
I have an ASP.NET page that displays work orders in a GridView. In that
GridView is a checkbox column. When the user clicks a "Print" button, I
create a report, using the .NET Framework printing...
|
by: it0ny |
last post by:
Hi guys,
thanks I am fairly new to this forum so I hope I chose the right place to post this question.
I try to make my program printout a deposit's report. I created a class to store the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |