470,815 Members | 2,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

sprintf: anomalous behavior

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

May 15 '06 #1
15 1552
>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
May 15 '06 #2
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
==============
May 15 '06 #3
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
May 15 '06 #4
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/
May 15 '06 #5

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

May 15 '06 #6
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
==============
May 15 '06 #7
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
==============
May 15 '06 #8
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:Th*************@gmail.com>

May 15 '06 #9

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

May 15 '06 #10
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/
May 15 '06 #11

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.

May 16 '06 #12
of course, but my question is why this istruction works on digital
while on HPUX no?
thanks
Ivano

May 16 '06 #13

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.

May 16 '06 #14
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
May 16 '06 #15
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/
May 16 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Huey | last post: by
3 posts views Thread by portergrouptx | last post: by
26 posts views Thread by steve | last post: by
1 post views Thread by jimjim | last post: by
12 posts views Thread by Henryk | last post: by
66 posts views Thread by gyan | last post: by
173 posts views Thread by Ron Ford | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.