sprintf:异常行为 [英] sprintf: anomalous behavior
问题描述
大家好,
我在不同的机器上使用sprintf函数发生了以下事情:
$ B $上的Digital Alpha
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,"%s%s",s1,s2) - >在HPUX上,s1等于PIPPOPLUTO
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,"%s%s",s1,s2) - > s1等于PLUTOPLUTO
有人知道我可以在HPUX上获得相同的行为吗?
谢谢所有
Ivano
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
推荐答案
>我在不同的
机器上使用sprintf函数发生了以下事情:<数字Alpha上的数据
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - >在HPUX上,s1等于PIPPOPLUTO
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - > s1等于PLUTOPLUTO
有人知道我可以在HPUX上获得相同的行为吗?
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?
如果你使用sprintf()来写它的一个输入字符串,你
调用未定义行为的愤怒。如果你写一个字符串
literal,你会调用未定义行为的愤怒。如果你在分配的内存结束时写下了
,你就会调出未定义
行为的愤怒。
请帮助摆脱这样糟糕的代码世界。请
删除该代码的所有副本,不要再写它了。
Gordon L. Burditt
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
Ivan78写道:
大家好,
我在不同的机器上使用sprintf函数发生了以下事情:
在Digital Alpha上/> s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - >在HPUX上,s1等于PIPPOPLUTO
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - > s1等于PLUTOPLUTO
有人知道我可以在HPUX上获得相同的行为吗?
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?
类似......
char * s1 =" PIPPO" ;
char * s2 =" PLUTO";
char buffer [sizeof(s1)+ sizeof(s2)+ 1];
sprintf(缓冲区,%s%s,s1,s2);
-
======= =======
不是学生
==============
Something like ...
char * s1 = "PIPPO" ;
char * s2 = "PLUTO";
char buffer[sizeof(s1) + sizeof(s2) + 1];
sprintf(buffer, "%s%s", s1, s2);
--
==============
Not a pedant
==============
Ivan78写道:
大家好,
我在不同的机器上使用sprintf函数发生了以下事情:
在Digital Alpha上
S1 = QUOT; PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - >在HPUX上,s1等于PIPPOPLUTO
s1 =" PIPPO"和s2 =PLUTO
sprintf(s1,%s%s,s1,s2) - > s1等于PLUTOPLUTO
有人知道我可以在HPUX上获得相同的行为吗?
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?
编写有效的C可能有所帮助。实际上,通过使s1既是输入到
sprintf,又将结果放在你调用未定义的
行为,所以字面上允许* * *就C
标准而言,就会发生。即便是你的Digital Alpha和HPUX机器也会因为
宣布相互之间的战争而且两者都被毁坏了。
灾难。
你也正在调用未定义的行为,因为你的调用试图修改一个字符串文字。
尝试使用足够大的char数组来保存结果作为第一个
sprintf的参数。
-
Flash Gordon,生活在有趣的时代。
网站 - http://home.flash-gordon.me.uk/
comp.lang.c发布指南和介绍:
http ://clc-wiki.net/wiki/Intro_to_clc
这篇关于sprintf:异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!