Using __LINE__ as a string

Discussion in 'C Programming' started by Paul Shipley, Oct 21, 2003.

  1. Paul Shipley

    Paul Shipley Guest

    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, Oct 21, 2003
    #1
    1. Advertising

  2. Paul Shipley <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I will never display my bum in public again."
    - Homer Simpson
     
    Joona I Palaste, Oct 21, 2003
    #2
    1. Advertising

  3. 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
     
    Hallvard B Furuseth, Oct 21, 2003
    #3
  4. 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
     
    Hallvard B Furuseth, Oct 21, 2003
    #4
  5. On 21 Oct 2003 03:46:59 -0700, (Paul Shipley) wrote:

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


    D:\source\clc>cat 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>031021_1
    line == 8

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

    line == __LINE__

    would be printed. :)

    -- Mat.
     
    Mathew Hendry, Oct 21, 2003
    #5
  6. Hallvard B Furuseth <h.b.furuseth(nospam)@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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I will never display my bum in public again."
    - Homer Simpson
     
    Joona I Palaste, Oct 21, 2003
    #6
  7. Paul Shipley

    Dan Pop Guest

    In <> (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:
     
    Dan Pop, Oct 21, 2003
    #7
  8. Paul Shipley

    Dan Pop Guest

    In <bn350j$2e4$> Joona I Palaste <> writes:

    >Hallvard B Furuseth <h.b.furuseth(nospam)@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:
     
    Dan Pop, Oct 21, 2003
    #8
  9. Greetings.

    In article <bn34ao$1sr$>, 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
     
    Tristan Miller, Oct 21, 2003
    #9
  10. 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?
     
    E. Robert Tisdale, Oct 21, 2003
    #10
  11. Paul Shipley

    rihad Guest

    On 21 Oct 2003 13:15:24 +0200, Hallvard B Furuseth
    <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote:

    >> 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.


    I would only guess that it's the blah blah blah part that takes care of that :)
    I saw this trick used while perusing the Apache httpd sourcecode: NULL on
    success, some message on error.
     
    rihad, Oct 21, 2003
    #11
  12. rihad wrote:

    >>> 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.

    >


    > I would only guess that it's the blah blah blah part that takes care
    > of that :)


    I don't see how it could. I didn't look at that part of the code,
    though. But that's _another_ bug: str_ptr is allocated at first, then
    it is assigned again without freeing the allocated memory. The blah
    blah blah code should fill in the already allocated memory, it shouldn't
    set str_ptr to something else.

    > I saw this trick used while perusing the Apache httpd sourcecode: NULL
    > on success, some message on error.


    That's fine. But there is no NULL to tell from any non-NULL in the OP's
    code.

    --
    Hallvard
     
    Hallvard B Furuseth, Oct 21, 2003
    #12
  13. Paul Shipley

    Nick Austin Guest

    On Tue, 21 Oct 2003 09:55:50 -0700, "E. Robert Tisdale"
    <> wrote:

    >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?


    http://www.eskimo.com/~scs/C-faq/q11.17.html

    Nick.
     
    Nick Austin, Oct 21, 2003
    #13
  14. Paul Shipley

    Dan Pop Guest

    In <> "E. Robert Tisdale" <> writes:

    >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?


    Are you merely reading impaired or a patent idiot?

    11.17: I'm trying to use the ANSI "stringizing" preprocessing operator
    `#' to insert the value of a symbolic constant into a message,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    but it keeps stringizing the macro's name rather than its value.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 22, 2003
    #14
  15. Paul Shipley

    Paul Shipley Guest

    (Dan Pop) wrote in message news:<bn3cao$86o$>...
    > In <bn350j$2e4$> Joona I Palaste <> writes:
    >
    > >Hallvard B Furuseth <h.b.furuseth(nospam)@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


    Thanks to all for your help.

    A bit about the target system: this code will eventually be running on
    an embedded system where ROM and RAM are clearly defined. RAM is
    limited - I've got 70K to play with. There isn't an operating system,
    so we wrap the memory access functions behind mem_alloc and mem_free.
    It is the responsibility of the calling function to free the memory
    using mem_free. Mem_free knows that if the pointer is pointing into
    ROM (i.e. is pointing at the "Out of memory message") then it doesn't
    have to do anything.

    I did get a solution:

    #define STR(x) #x
    #define XSTR(x) STR(x)
    buf = "Out of memory in " __FILE__ ":" XSTR(__LINE__) "\n";

    And yes, it was in the FAQ, but rest assured my chagrined head is now
    perspicuously hanging in shame!!!

    Thanks,
    Paul.
     
    Paul Shipley, Oct 22, 2003
    #15
  16. Paul Shipley

    Paul Shipley Guest

    Mathew Hendry <> wrote in message news:<>...
    > On 21 Oct 2003 03:46:59 -0700, (Paul Shipley) wrote:
    >
    > >Any suggestions on how to convert __LINE__ to a string at compile time
    > >will be most appreciated.

    >
    > D:\source\clc>cat 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>031021_1
    > line == 8
    >
    > You must go through two macros to force expansion of __LINE__, otherwise
    >
    > line == __LINE__
    >
    > would be printed. :)
    >
    > -- Mat.


    Ah - perfect! Just what I needed. Thanks, Mathew.

    Paul.
     
    Paul Shipley, Oct 22, 2003
    #16
  17. Paul Shipley

    Default User Guest

    Dan Pop wrote:

    > Are you merely reading impaired or a patent idiot?



    You left out troll.




    Brian Rodenborn
     
    Default User, Oct 22, 2003
    #17
  18. Paul Shipley

    CBFalconer Guest

    Dan Pop wrote:
    > "E. Robert Tisdale" <> writes:
    >

    .... snip ...
    > >
    > > Exactly which FAQ answers this question?

    >
    > Are you merely reading impaired or a patent idiot?


    Do you really need a response to that question?

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 22, 2003
    #18
  19. Paul Shipley

    Dan Pop Guest

    In <> CBFalconer <> writes:

    >Dan Pop wrote:
    >> "E. Robert Tisdale" <> writes:
    >>

    >... snip ...
    >> >
    >> > Exactly which FAQ answers this question?

    >>
    >> Are you merely reading impaired or a patent idiot?

    >
    >Do you really need a response to that question?


    Nope, it was purely rhetorical ;-)

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 23, 2003
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imre
    Replies:
    4
    Views:
    662
    Peter Gordon
    Feb 23, 2005
  2. Replies:
    2
    Views:
    527
    Raymond Martineau
    Feb 23, 2005
  3. Spry
    Replies:
    1
    Views:
    981
    Richard Heathfield
    Jul 26, 2003
  4. qazmlp

    Definition of __FILE__, __LINE__

    qazmlp, Aug 9, 2003, in forum: C Programming
    Replies:
    9
    Views:
    647
    Mark McIntyre
    Aug 19, 2003
  5. Replies:
    3
    Views:
    599
    Bo Persson
    Aug 15, 2007
Loading...

Share This Page