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? 5 1791
Carlos wrote: I have a macro
[...]
Only a small correction, please answer to the parent taking this into
account:
Changing DIE do
#define DIE(msg, line) fputs(msg #lines, stderr)
#line
Thanks.
Carlos wrote:
[...] 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?
Sorry for wasting your time :). Immediatly after posting that I found
the solution:
$ cat pre.c
#define DIE3(msg, line) fputs(msg #line, stderr)
#define DIE2(msg, line) DIE3(msg, line)
#define DIE(msg) DIE2(msg, __LINE__)
DIE("error");
DIE("error");
$ gcc -E pre.c
# 1 "pre.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "pre.c"
fputs("error" "5", stderr);
fputs("error" "6", stderr);
$
:-)
On Sat, 30 Jul 2005 00:18:10 +0200, Carlos wrote: Carlos wrote: Immediatly after posting that I found the solution:
$ cat pre.c #define DIE3(msg, line) fputs(msg #line, stderr) #define DIE2(msg, line) DIE3(msg, line) #define DIE(msg) DIE2(msg, __LINE__)
Or more generally:
#define STR0(s) #s
#define STR1(s) STR0(s)
#define LINE_STRING STR1(__LINE__)
#define DIE(msg) fputs(msg LINE_STRING, stderr)
Mike
In article <dc**********@d omitilla.aioe.o rg>, Carlos wrote: 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!");
Look up the assert macro in assert.h
copy what it does.
Bye.
Jasen
In article <dc**********@d omitilla.aioe.o rg>, Carlos <an***@quovadis .com.ar> writes: it is possible that the fprintf will fail too, trying to allocate memory to build the output string
Or for any other reason. The language doesn't guarantee that output
will succeed. It's possible that there are cases where fprintf would
fail but fputs would succeed; it's even possible, though I think it
unlikely, that your program would encounter one. Personally, I
wouldn't consider it high on my list of potential problems to
address.
and also, I think in Linux mallocs never fail, but anyway).
<OT>
About that you are wrong. man setrlimit.
</OT>
--
Michael Wojcik mi************@ microfocus.com
The antics which have been drawn together in this book are huddled here
for mutual protection like sheep. If they had half a wit apiece each
would bound off in many directions, to unsimplify the target. -- Walt Kelly This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Imre |
last post by:
Is there a Visual C++ newsgroup? I guess this question should go there,
but I couldn't find it.
Please take a look at the following little program:
template <int Line>
struct Test
{
enum { value = Line };
};
|
by: Dom Gilligan |
last post by:
Is there any way to get the preprocessor to produce the current line
number in double quotes? At first sight, gcc seems to replace __LINE__
last (which would make sense), and so won't replace it at all if it's
preceded by '#'.
Background: I want to produce a string giving the current file and
line number in an array of structures, as follows:
---------------
|
by: Spry |
last post by:
Hi,
I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.
The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)
Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.
|
by: Paul Shipley |
last post by:
Hi,
Does anyone know a way of converting the __LINE__ macro to a string at
compile time? The reason I ask it because I want to put some debug
information in to tell me if memory is not being allocated. For
example, this function will return the status of my system:
static char* get_status (void)
{
char* str_ptr;
|
by: jake1138 |
last post by:
I couldn't find an example of this anywhere so I post it in the hope
that someone finds it useful. I believe this is compiler specific (I'm
using gcc), as C99 defines __VA_ARGS__. Comments are welcome.
This will print the file name and line number followed by a format
string and a variable number of arguments. The key here is that you
MUST have a space between __LINE__ and the last comma, otherwise
__LINE__ gets eaten by the ## if...
| |
by: Francois Grieu |
last post by:
Hello,
I wrote this:
#define M(x) enum { m##__LINE__ = x };
#line 1000
M(126)
M(341)
M(565)
...
|
by: David Bear |
last post by:
I'm rather new to pickling but I have some dictionaries and lists I want to
package and send to another process (on another machine).
I was hoping I could just send a stringified pickle. However, the examples
in the doc have:
>>> import pickle
>>> pickle.dump(obj,open('save.p','w'))
I don't really want to write to a file. I know I could write to sys.stdout.
|
by: Generic Usenet Account |
last post by:
Can someone kindly explain why stringification of the compiler
preprocessor macro __LINE__ requires two steps, instead of one? I
wanted to pass the error location of a system call to perror() and I
found that I had to use a kludgy way to stick in the line number.
Thanks,
Song
///// Code Snippet /////
#include <stdio.h>
|
by: travis.downs |
last post by:
Hi,
I'm trying to use a macro to create a unique temporary variable name,
such as
#define TEMP_OBJ(string) MyType obj_ <some magic here(string);
So something like
TEMP_OBJ("foo")
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |