sprintf: anomalous behavior

Discussion in 'C Programming' started by Ivan78, May 15, 2006.

  1. Ivan78

    Ivan78 Guest

    Hi everybody,
    I happened the following thing using sprintf function on different
    machine :
    on Digital Alpha
    s1="PIPPO" and s2="PLUTO"
    sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    while on HPUX
    s1="PIPPO" and s2="PLUTO"
    sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO

    somebody knows as can i get the same behavior on HPUX to?

    thanks all
    Ivano
     
    Ivan78, May 15, 2006
    #1
    1. Advertising

  2. >I happened the following thing using sprintf function on different
    >machine :
    > on Digital Alpha
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    > while on HPUX
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >
    >somebody knows as can i get the same behavior on HPUX to?


    If you use sprintf() to write on one of its input strings, you
    invoke the wrath of undefined behavior. If you write on a string
    literal, you invoke the wrath of undefined behavior. If you write
    off the end of allocated memory, you invoke the wrath of undefined
    behavior.

    Please help rid the world of awful code like that. Please
    delete all copies of that code and don't write any more like it.

    Gordon L. Burditt
     
    Gordon Burditt, May 15, 2006
    #2
    1. Advertising

  3. Ivan78

    pemo Guest

    Ivan78 wrote:
    > Hi everybody,
    > I happened the following thing using sprintf function on different
    > machine :
    > on Digital Alpha
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    > while on HPUX
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >
    > somebody knows as can i get the same behavior on HPUX to?


    Something like ...

    char * s1 = "PIPPO" ;
    char * s2 = "PLUTO";

    char buffer[sizeof(s1) + sizeof(s2) + 1];

    sprintf(buffer, "%s%s", s1, s2);


    --
    ==============
    Not a pedant
    ==============
     
    pemo, May 15, 2006
    #3
  4. Ivan78

    Flash Gordon Guest

    Ivan78 wrote:
    > Hi everybody,
    > I happened the following thing using sprintf function on different
    > machine :
    > on Digital Alpha
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    > while on HPUX
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >
    > somebody knows as can i get the same behavior on HPUX to?


    Writing valid C might help. As it is, by making s1 both an input to
    sprintf and the array it puts the result in you are invoking undefined
    behaviour, so literally *anything* is allowed to happen as far as the C
    standard is concerned. Even your Digital Alpha and HPUX machines
    declaring war on each other and both being destroyed in the resultant
    catastrophe.

    You are also invoking undefined behaviour because your call attempts to
    modify a string literal.

    Try using a char array large enough to hold the results as the first
    parameter to sprintf.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 15, 2006
    #4
  5. Ivan78

    Marc Thrun Guest

    pemo schrieb:
    > Ivan78 wrote:
    >> Hi everybody,
    >> I happened the following thing using sprintf function on different
    >> machine :
    >> on Digital Alpha
    >> s1="PIPPO" and s2="PLUTO"
    >> sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    >> while on HPUX
    >> s1="PIPPO" and s2="PLUTO"
    >> sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >>
    >> somebody knows as can i get the same behavior on HPUX to?

    >
    > Something like ...
    >
    > char * s1 = "PIPPO" ;
    > char * s2 = "PLUTO";
    >
    > char buffer[sizeof(s1) + sizeof(s2) + 1];
    >
    > sprintf(buffer, "%s%s", s1, s2);
    >
    >


    ITYM

    char s1[] = "PIPPO";
    char s2[] = "PLUTO";

    Otherwise sizeof(s1) and sizeof(s2) will yield the size of a char*, not
    the char array.

    --
    Marc Thrun
    http://www.tekwarrior.de/
     
    Marc Thrun, May 15, 2006
    #5
  6. Marc Thrun wrote:
    > pemo schrieb:
    > > Something like ...
    > > char * s1 = "PIPPO" ;
    > > char * s2 = "PLUTO";
    > > char buffer[sizeof(s1) + sizeof(s2) + 1];
    > > sprintf(buffer, "%s%s", s1, s2);

    > char s1[] = "PIPPO";
    > char s2[] = "PLUTO";
    > Otherwise sizeof(s1) and sizeof(s2) will yield the size of a char*, not
    > the char array.


    I think what pemo really meant is strlen instead of sizeof, but wrote
    it wrongly carelessly. Secondly, if s1 and s2 are char [] as suggested,
    then buffer size is not exact correct, is more larger than needed.

    If I am wrong also, please correct me.

    --
    lovecreatesbeauty
     
    lovecreatesbeauty, May 15, 2006
    #6
  7. Ivan78

    pemo Guest

    Marc Thrun wrote:
    > pemo schrieb:
    >> Ivan78 wrote:
    >>> Hi everybody,
    >>> I happened the following thing using sprintf function on different
    >>> machine :
    >>> on Digital Alpha
    >>> s1="PIPPO" and s2="PLUTO"
    >>> sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    >>> while on HPUX
    >>> s1="PIPPO" and s2="PLUTO"
    >>> sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >>>
    >>> somebody knows as can i get the same behavior on HPUX to?

    >>
    >> Something like ...
    >>
    >> char * s1 = "PIPPO" ;
    >> char * s2 = "PLUTO";
    >>
    >> char buffer[sizeof(s1) + sizeof(s2) + 1];
    >>
    >> sprintf(buffer, "%s%s", s1, s2);
    >>
    >>

    >
    > ITYM
    >
    > char s1[] = "PIPPO";
    > char s2[] = "PLUTO";
    >
    > Otherwise sizeof(s1) and sizeof(s2) will yield the size of a char*,
    > not the char array.


    Yes, thanks for the correction.

    It seems that I've gotten into a bit of a habit of this lately - attempting
    to help/answer, yet posting nonsense.


    --
    ==============
    Not a pedant
    ==============
     
    pemo, May 15, 2006
    #7
  8. Ivan78

    pemo Guest

    lovecreatesbeauty wrote:
    > Marc Thrun wrote:
    >> pemo schrieb:
    >>> Something like ...
    >>> char * s1 = "PIPPO" ;
    >>> char * s2 = "PLUTO";
    >>> char buffer[sizeof(s1) + sizeof(s2) + 1];
    >>> sprintf(buffer, "%s%s", s1, s2);

    >> char s1[] = "PIPPO";
    >> char s2[] = "PLUTO";
    >> Otherwise sizeof(s1) and sizeof(s2) will yield the size of a char*,
    >> not the char array.

    >
    > I think what pemo really meant is strlen instead of sizeof, but wrote
    > it wrongly carelessly.


    <snip>

    > If I am wrong also, please correct me.


    Although I certainly know what sizeof(s1) will equate to, I sadly *did* mean
    sizeof. I'm just being dumber than I normally am lately!

    --
    ==============
    Not a pedant
    ==============
     
    pemo, May 15, 2006
    #8
  9. Ivan78 wrote:
    >
    > Hi everybody,
    > I happened the following thing using sprintf function on different
    > machine :
    > on Digital Alpha
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PIPPOPLUTO
    > while on HPUX
    > s1="PIPPO" and s2="PLUTO"
    > sprintf(s1, "%s%s", s1,s2) -> s1 is equal PLUTOPLUTO
    >
    > somebody knows as can i get the same behavior on HPUX to?


    strcat(s1,s2);

    Assuming, of course, that s1 is / points to a modifiable buffer which is
    large enough to hold the result.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, May 15, 2006
    #9
  10. pemo wrote:
    > Although I certainly know what sizeof(s1) will equate to, I sadly *did* mean
    > sizeof. I'm just being dumber than I normally am lately!


    But I wonder why not use strlen? Two sizeofs count two more null chars
    in.

    char *s1 = "PIPPO";
    char *s2 = "PLUTO";
    char buffer[strlen(s1) + strlen(s2) + 1];

    buffer : PIPPOPLUTO 11

    char s1_2[] = "PIPPO" ;
    char s2_2[] = "PLUTO";
    char buffer_2[sizeof(s1_2) + sizeof(s2_2) + 1];

    buffer_2: PIPPOPLUTO 13

    --
    lovecreatesbeauty
     
    lovecreatesbeauty, May 15, 2006
    #10
  11. Ivan78

    Marc Thrun Guest

    lovecreatesbeauty schrieb:
    > pemo wrote:
    >> Although I certainly know what sizeof(s1) will equate to, I sadly *did* mean
    >> sizeof. I'm just being dumber than I normally am lately!

    >
    > But I wonder why not use strlen? Two sizeofs count two more null chars
    > in.
    >
    > char *s1 = "PIPPO";
    > char *s2 = "PLUTO";
    > char buffer[strlen(s1) + strlen(s2) + 1];
    >
    > buffer : PIPPOPLUTO 11
    >
    > char s1_2[] = "PIPPO" ;
    > char s2_2[] = "PLUTO";
    > char buffer_2[sizeof(s1_2) + sizeof(s2_2) + 1];
    >
    > buffer_2: PIPPOPLUTO 13
    >
    > --
    > lovecreatesbeauty
    >


    Because sizeof evaluates to a compile time constant. This can be used to
    declare an array even in non C99 where variable length arrays were
    introduced. In pre-C99 implementations when using strlen() you have to
    use malloc() to allocate sufficient memory.

    --
    Marc Thrun
    http://www.tekwarrior.de/
     
    Marc Thrun, May 15, 2006
    #11
  12. Marc Thrun wrote:
    > Because sizeof evaluates to a compile time constant. This can be used to
    > declare an array even in non C99 where variable length arrays were
    > introduced. In pre-C99 implementations when using strlen() you have to
    > use malloc() to allocate sufficient memory.


    Is it suggested that I had better not to write the most/latest
    standard-compliant code for better backwark compatibility? If so, what
    is the value of the various computer programming language standards.
     
    lovecreatesbeauty, May 16, 2006
    #12
  13. Ivan78

    Ivan78 Guest

    of course, but my question is why this istruction works on digital
    while on HPUX no?
    thanks
    Ivano
     
    Ivan78, May 16, 2006
    #13
  14. Ivan78

    Flash Gordon Guest

    Ivan78 wrote:
    > of course, but my question is why this istruction works on digital
    > while on HPUX no?


    Please provide context. Most people do not use Google and there is no
    guarantee that people have easy access (or have ever seen) the post you
    are replying to. See the Google section at
    http://clc-wiki.net/wiki/Intro_to_clc and the pages it links to.

    I'm guessing you are talking about unsigned behaviour. Try crossing the
    road wearing a blind fold and ear defenders. If you don't get run over
    consider why. The answer is blind luck. See
    http://clc-wiki.net/wiki/Undefined_behaviour
    Also search the group for "undefined behaviour".
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 16, 2006
    #14
  15. Ivan78

    Vladimir Oka Guest

    Ivan78 wrote:
    > of course, but my question is why this istruction works on digital
    > while on HPUX no?
    > thanks
    > Ivano


    Please quote context. Read: <http://cfaj.freeshell.org/google/>

    In reply to your question: that's the beauty of undefined behaviour. It
    needs no reason why, literally /anything/ can happen (look up "demons
    flying out of your nose"). Your implementations are not evenrequired to
    be consistent (i.e. today it may work in one way, tomorrow it may blow
    up a small island).

    In real life, if you dig deep into how your two implementations are
    designed, you may be able to see the exact mechanism that produces one
    behaviour or another. Still, relying on any of this would be a Bad
    Thing.
     
    Vladimir Oka, May 16, 2006
    #15
  16. Ivan78

    Marc Thrun Guest

    lovecreatesbeauty schrieb:
    > Marc Thrun wrote:
    >> Because sizeof evaluates to a compile time constant. This can be used to
    >> declare an array even in non C99 where variable length arrays were
    >> introduced. In pre-C99 implementations when using strlen() you have to
    >> use malloc() to allocate sufficient memory.

    >
    > Is it suggested that I had better not to write the most/latest
    > standard-compliant code for better backwark compatibility? If so, what
    > is the value of the various computer programming language standards.
    >


    Many modern C implementations still do not support C99 features, or
    support them in a different manner. Therefore it is reasonable to write
    code compilable in C99 implementations and pre-C99 implementations for
    increased portability.

    --
    Marc Thrun
    http://www.tekwarrior.de/
     
    Marc Thrun, May 16, 2006
    #16
    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,599
    shea martin
    Sep 3, 2004
  2. Mantorok Redgormor
    Replies:
    70
    Views:
    1,764
    Dan Pop
    Feb 17, 2004
  3. Guest
    Replies:
    0
    Views:
    285
    Guest
    Mar 13, 2006
  4. Xcriber51

    Weird sprintf behavior

    Xcriber51, Nov 10, 2007, in forum: C Programming
    Replies:
    15
    Views:
    639
    Xcriber51
    Nov 11, 2007
  5. Ben Gribaudo

    Unexpected sprintf/String % Behavior

    Ben Gribaudo, Jun 15, 2005, in forum: Ruby
    Replies:
    3
    Views:
    175
    Ben Gribaudo
    Jun 17, 2005
Loading...

Share This Page