|
|
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 |
|
|
>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 |
|
|
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); |
|
|
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. |
|
|
pemo schrieb:
> Ivan78 wrote: > 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 wrote:
> pemo schrieb: > 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. |
|
|
Marc Thrun wrote:
> pemo schrieb: > 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. |
|
|
lovecreatesbeauty wrote:
> Marc Thrun wrote: > 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! |
|
|
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. |
|
|
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 schrieb:
[..] > 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 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. |
|
|
of course, but my question is why this istruction works on digital
while on HPUX no? thanks Ivano |
|
|
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 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 Also search the group for "undefined behaviour". |
|
|
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. |