Re: Append with sprintf

Discussion in 'C Programming' started by Ersek, Laszlo, Jun 4, 2010.

  1. On Fri, 4 Jun 2010, Guillaume Dargaud wrote:

    > Hello all,
    > this is something I commonly do, but I have a nagging doubt that it may not
    > be correct and/or dangerous in some situations:
    >
    > // Append
    > sprintf(Str, "%s (Id=%d, Pnl=%d)", Str, Id, Pnl);
    >
    > I know from experience (and _some_ sense of logic) that the following is a
    > no-no:
    > // Prepend
    > sprintf(Str, "(Id=%d, Pnl=%d) %s", Id, Pnl, Str);
    >
    > Comments ?


    The "Append" is just as no-no: (a) the first parameter of sprintf() is a
    restrict-qualified pointer, (b) looking at the May 6, 2005 Committee
    Draft, 7.19.6.6 p2 explicitly states, "If copying takes place between
    objects that overlap, the behavior is undefined."

    Know the lengths of your strings and the sizes of the arrays holding your
    strings. Drepper is right.

    http://sources.redhat.com/ml/libc-alpha/2000-08/msg00053.html
    http://sources.redhat.com/ml/libc-alpha/2000-08/msg00061.html

    Cheers,
    lacos
     
    Ersek, Laszlo, Jun 4, 2010
    #1
    1. Advertising

  2. Ersek, Laszlo

    Fire Crow Guest

    On Jun 4, 12:06 pm, William Ahern <will...@wilbur.25thandClement.com>
    wrote:
    [snip]
    >. As with most languages
    > working with text is a very common task, and in C the normative type for
    > text is pointer-to-char, and the normative interfaces the str* and printf
    > interfaces.
    >


    I agree with that for reading strings, but for writing/editing/
    concatenating and copying
    id like to know the length in advance. i think it's nice to know hte
    length and put the
    NUL byte in as well. for the above stated reason.

    +
    | fire crow
    | computer programmer
    !_____________________

    http://firecrow.com, , 917.306.9451, New York City,
    USA
     
    Fire Crow, Jun 4, 2010
    #2
    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. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,608
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,084
  3. CJ
    Replies:
    1
    Views:
    1,355
    Davlet Panech
    Oct 28, 2003
  4. HYRY
    Replies:
    10
    Views:
    627
    Bruno Desthuilliers
    Sep 26, 2007
  5. Tom St Denis

    Re: Append with sprintf

    Tom St Denis, Jun 4, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,514
    Nobody
    Aug 24, 2010
Loading...

Share This Page