473,804 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using __LINE__ as a string

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;
str_ptr = mem_alloc( SIZE_OF_TEXT );

if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";
}
else
{
str_ptr = /* blah blah blah... */
}
return( str_ptr);
}

Of course this code won't work, because __LINE__ is an integer, not a
string. Also, I can't resort to something like printf( "%u \n",
__LINE__ ) because in this case the output might not (and probably
won't) be going to stdout. Also, sprintf can't be used because there
is no memory available to sprintf to.

Any suggestions on how to convert __LINE__ to a string at compile time
will be most appreciated.

Thanks,
Paul.
Nov 13 '05 #1
18 11396
Paul Shipley <pa**********@s evcon.com> scribbled the following:
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;
str_ptr = mem_alloc( SIZE_OF_TEXT ); if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";
sprintf(str_ptr , "Out of memory in %s at %d\n", __FILE__, __LINE__);
}
else
{
str_ptr = /* blah blah blah... */
}
return( str_ptr);
}


--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Nov 13 '05 #2
Paul Shipley wrote:
Hi,

Does anyone know a way of converting the __LINE__ macro to a string at
compile time? (...)
#define NOMEM() NOMEM__1(__LINE __)
#define NOMEM__1(li) NOMEM__2(li)
#define NOMEM__2(li) "Out of memory in " __FILE__ " at " #li "\n"

'#li' converts the _unexpanded_ argument to a string. That's why
three macros are needed: This
#define NOMEM__1(li) "Out of memory in " __FILE__ " at " #li "\n"
would produce "Out of memory in " "foo.c" " at " "__LINE__" "\n"
static char* get_status (void)
{
char* str_ptr;
str_ptr = mem_alloc( SIZE_OF_TEXT );

if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";
str_ptr = NOMEM()
}
else
{
str_ptr = /* blah blah blah... */
}
return( str_ptr);
}


However, this program has a bug. How will the caller know whether
or not to free the returned memory? It can return either memory
from mem_alloc() or a static string.

--
Hallvard
Nov 13 '05 #3
Joona I Palaste wrote:
str_ptr = mem_alloc( SIZE_OF_TEXT );
if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";


sprintf(str_ptr , "Out of memory in %s at %d\n", __FILE__, __LINE__);


Not a good idea when str_ptr == NULL.
Besides, we don't know if SIZE_OF_TEXT is big enough.

--
Hallvard
Nov 13 '05 #4
On 21 Oct 2003 03:46:59 -0700, pa**********@se vcon.com (Paul Shipley) wrote:
Any suggestions on how to convert __LINE__ to a string at compile time
will be most appreciated.


D:\source\clc>c at 031021_1.c
#include <stdio.h>

#define STRX(x) #x
#define STR(x) STRX(x)

int main(void)
{
const char *line = STR(__LINE__);
printf("line == %s\n", line);
return 0;
}

D:\source\clc>0 31021_1
line == 8

You must go through two macros to force expansion of __LINE__, otherwise

line == __LINE__

would be printed. :)

-- Mat.

Nov 13 '05 #5
Hallvard B Furuseth <h.b.furuseth(n ospam)@usit.uio (nospam).no> scribbled the following:
Joona I Palaste wrote:
str_ptr = mem_alloc( SIZE_OF_TEXT );
if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";
sprintf(str_ptr , "Out of memory in %s at %d\n", __FILE__, __LINE__);

Not a good idea when str_ptr == NULL.
Besides, we don't know if SIZE_OF_TEXT is big enough.


Whoops. Misread == as !=. Thanks for the correction. Well, if we assume
that SIZE_OF_TEXT is absolute bloody big, then we could malloc() a
smaller amount here and sprintf() into it.
After all, there has to be enough memory to hold the "Out of memory"
message somewhere. If not, we're screwed even with the original
solution.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
Nov 13 '05 #6
In <bn**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
Hallvard B Furuseth <h.b.furuseth(n ospam)@usit.uio (nospam).no> scribbled the following:
Joona I Palaste wrote:
str_ptr = mem_alloc( SIZE_OF_TEXT );
if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";

sprintf(str_ptr , "Out of memory in %s at %d\n", __FILE__, __LINE__);

Not a good idea when str_ptr == NULL.
Besides, we don't know if SIZE_OF_TEXT is big enough.


Whoops. Misread == as !=. Thanks for the correction. Well, if we assume
that SIZE_OF_TEXT is absolute bloody big, then we could malloc() a
smaller amount here and sprintf() into it.
After all, there has to be enough memory to hold the "Out of memory"
message somewhere. If not, we're screwed even with the original
solution.


If not, one of two things will happen:

1. The program will fail to translate.

2. The program will fail to load.

In either case, the program cannot be executed. That's the big advantage
of static memory allocation: you don't spend hours and hours of execution
time only to be told that the program cannot allocate some memory it
needs in order to continue to execute.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
In <5a************ **************@ posting.google. com> pa**********@se vcon.com (Paul Shipley) writes:
Any suggestions on how to convert __LINE__ to a string at compile time
will be most appreciated.


Ever considered reading the FAQ *before* posting?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
Greetings.

In article <bn**********@o ravannahka.hels inki.fi>, Joona I Palaste wrote:
sprintf(str_ptr , "Out of memory in %s at %d\n", __FILE__, __LINE__);


Complicated. You will need to calculate a base-10 logarithm and one or more
strlen()s to make sure that the str_ptr has enough memory allocated.
Unless you really need the string in dynamic memory, it's better to use the
preprocessor to generate a constant string on the fly.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #9
Dan Pop wrote:
Paul Shipley writes:
Any suggestions on how to convert __LINE__ to a string at compile time
will be most appreciated.


Ever considered reading the FAQ *before* posting?


Exactly which FAQ answers this question?

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8370
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: ---------------
1
6949
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.
9
25264
by: qazmlp | last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the definition of these macros implementation dependent ? I am wondering how easily they can get the file name and line number respectively.
6
1698
by: Gary Morris | last post by:
Hi all, I tried posting this through a free news server, but it still has not appeared in Google, so if it turns up again I apologize. I hope someone can help me with this, or at least help me find some information that will help me. If I were not at my wit's end already, I wouldn't even ask. I'm used to doing all of my programming in Windows, but now I have a task to
9
2344
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) ...
16
3284
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to build this "compact matrix" (see later).
5
579
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>
3
7818
by: pistmaster | last post by:
Hi, I am trying the use the current line number in my logging and I want to stringify it so that I know the size of the buffer required to output the log string. I would have though I could use #__LINE__ in a macro like: #define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__) but all i get in the output is #<linenumber>. Why should this be?
3
14581
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")
0
10350
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
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,...
0
10096
tracyyun
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...
0
9174
agi2029
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...
1
7638
isladogs
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...
0
5534
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
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
3
3002
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.