sprintf: anomalous behavior

I

Ivan78

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
 
G

Gordon Burditt

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
 
P

pemo

Ivan78 said:
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);
 
F

Flash Gordon

Ivan78 said:
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.
 
M

Marc Thrun

pemo said:
Ivan78 said:
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.
 
L

lovecreatesbeauty

Marc said:
pemo said:
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.
 
P

pemo

Marc said:
pemo said:
Ivan78 said:
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.
 
P

pemo

lovecreatesbeauty said:
Marc said:
pemo said:
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.

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!
 
K

Kenneth Brody

Ivan78 said:
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:[email protected]>
 
L

lovecreatesbeauty

pemo said:
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
 
M

Marc Thrun

lovecreatesbeauty said:
pemo said:
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

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

lovecreatesbeauty

Marc said:
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.
 
I

Ivan78

of course, but my question is why this istruction works on digital
while on HPUX no?
thanks
Ivano
 
F

Flash Gordon

Ivan78 said:
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".
 
V

Vladimir Oka

Ivan78 said:
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.
 
M

Marc Thrun

lovecreatesbeauty said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top