使用__LINE__作为字符串 [英] Using __LINE__ as a string

查看:114
本文介绍了使用__LINE__作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有人知道在_ / $
编译时将__LINE__宏转换为字符串的方法?我问它的原因是因为我想把一些调试

信息告诉我是否没有分配内存。对于

示例,此函数将返回我的系统状态:

静态字符* get_status(无效)

{

char * str_ptr;

str_ptr = mem_alloc(SIZE_OF_TEXT);


if(NULL == str_ptr)

{

str_ptr ="内存不足 __FILE__在 __LINE__" \ n";

}

其他

{

str_ptr = / * blah blah blah ...... * /

}

返回(str_ptr);

}


当然这段代码不起作用,因为__LINE__是一个整数,而不是一个

字符串。另外,我不能使用printf(%u \ n,

__LINE__)之类的东西,因为在这种情况下输出可能不会(并且可能是

不会去stdout。此外,sprintf不能使用,因为那里没有
没有可用于sprintf的内存。


有关如何将__LINE__转换为字符串的任何建议编译时间

将非常感激。


谢谢,

保罗。

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.

推荐答案

Paul Shipley< pa ********** @ sevcon.com>潦草地写道:
Paul Shipley <pa**********@sevcon.com> scribbled the following:

有没有人知道在编译时将__LINE__宏转换为字符串的方法?我问它的原因是因为我想把一些调试信息告诉我是否没有分配内存。对于
示例,此函数将返回我的系统状态:
static char * get_status(void)
{*> char * str_ptr;
str_ptr = mem_alloc(SIZE_OF_TEXT );
if(NULL == str_ptr)
{
str_ptr ="内存不足 __FILE__在 __LINE__" \ n" ;;


sprintf(str_ptr,%s中的内存不足%d\ n,__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _否则
{
str_ptr = / *等等等等...... * /
}
返回(str_ptr);
}
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);
}


< br $> b $ b -

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

我再也不会公开露面了。

- 荷马辛普森



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson


Paul Shipley写道:
Paul Shipley wrote:


有没有人知道如何将__LINE__宏转换为字符串
编译时间? (...)


#define NOMEM()NOMEM__1(__ LINE__)

#define NOMEM__1(li)NOMEM__2(li)

#define NOMEM__2(li)Out of memory in __FILE__在 #li" \ n"


''#li''将_unexpanded_参数转换为字符串。这就是为什么

需要三个宏:这个
"

会产生内存不足 " foo.c的" "在 " __ LINE __" " \ n"

static char * get_status(void)
{*> char * str_ptr;
str_ptr = mem_alloc(SIZE_OF_TEXT);

if(NULL == str_ptr)
{
str_ptr ="内存不足 __FILE__在 __LINE__" \ n" ;;


str_ptr = NOMEM()

}


{
str_ptr = / *等等等等等等* /
}
返回(str_ptr);
}
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);
}




但是,这个程序有一个错误。调用者如何知道

是否可以释放返回的内存?它可以从mem_alloc()返回内存

或静态字符串。


-

Hallvard



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


Joona I Palaste写道:
Joona I Palaste wrote:
str_ptr = mem_alloc(SIZE_OF_TEXT);
if(NULL == str_ptr)
{
str_ptr ="内存不足 __FILE__在 __LINE__" \ n";
str_ptr = mem_alloc( SIZE_OF_TEXT );
if( NULL == str_ptr )
{
str_ptr = "Out of memory in " __FILE__ " at " __LINE__ "\n";



sprintf(str_ptr,"%s中的内存不足,%d \ n",__ FILE __,_ _ _ _ _ _ _);



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




当str_ptr == NULL时不是一个好主意。

此外,我们不知道SIZE_OF_TEXT是否足够大。

-

Hallvard



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

--
Hallvard


这篇关于使用__LINE__作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆