about sprintf function

H

hongky gump

#include <stdio.h>
#include <string.h>

char str[128];
char append[128];
....
sprintf(str, "%s%s", str, append);
....

is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

msn: (e-mail address removed)
 
T

Taran

hongky said:
#include <stdio.h>
#include <string.h>
char str[128];
char append[128];
sprintf(str, "%s%s", str, append);
is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

There's a logical error.

str is of 128 and so is 'append'. When you append 'append' to 'str' it
will write to some memory locations beyond 'str' and corrupt them. The
overwriting can 0 to 128 locations, 0 when str is empty first element
is '\0' and 128 when last element is '\0'.

This is why the result is different.

To see for your self:
******************************

char str[12]="some__string";
char append[12]="same__string";
char *next=str+(sizeof(char)*(sizeof(str)+sizeof(append)));
*next='Z';
printf("before copy %c\n",*next);
sprintf(str, "%s%s", str, append);
printf("after copy %c\n",*next);

******************************
I didn't chk the result on gcc.
Hope this helps.

Regards,
Taran Tripathi
 
S

S.Tobias

hongky gump said:
#include <stdio.h>
#include <string.h>
char str[128];
char append[128];
...
sprintf(str, "%s%s", str, append);
...
is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

In C99:
int sprintf(char * restrict s, const char * restrict format, ...);
^^^^^^^^
In C90 it was UB "If copying takes place between objects that overlap".
 
D

dandelion

hongky gump said:
#include <stdio.h>
#include <string.h>

char str[128];
char append[128];
...
sprintf(str, "%s%s", str, append);
...

is it standard use?

Fortunately not.
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

Probably str gets overwritten while still being used.
 
T

Taran

hongky said:
#include <stdio.h>
#include <string.h>
char str[128];
char append[128];
sprintf(str, "%s%s", str, append);
is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

There's a logical error.

str is of 128 and so is 'append'. When you append 'append' to 'str' it
will write to some memory locations beyond 'str' and corrupt them. The
overwriting can 0 to 128 locations, 0 when str is empty first element
is '\0' and 128 when last element is '\0'.

This is why the result is different.

To see for your self:
******************************

char str[12]="some__string";
char append[12]="same__string";
char *next=str+(sizeof(char)*(sizeof(str)+sizeof(append)));
*next='Z';
printf("before copy %c\n",*next);
sprintf(str, "%s%s", str, append);
printf("after copy %c\n",*next);

******************************
I didn't chk the result on gcc.

Hope this helps.
 
L

Lawrence Kirby

hongky said:
#include <stdio.h>
#include <string.h>
char str[128];
char append[128];
sprintf(str, "%s%s", str, append);
is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

There's a logical error.

str is of 128 and so is 'append'. When you append 'append' to 'str' it
will write to some memory locations beyond 'str' and corrupt them. The

The arrays are, that doesn't mean that the strings they contain are
(assuming that they contain valid strings which isn't clear from the code).
It is entirely possible that the result of appending append to str will
fit completely in str. However this is certainly a potential source of
error.
overwriting can 0 to 128 locations, 0 when str is empty first element
is '\0' and 128 when last element is '\0'.

There is a more immediate problem in that source and destination buffers
for sprintf() overlap, i.e. str is both read from and written to. This has
undefined behaviour. It makes more sense here to use

strcat(str, append);

And, yes, to check that the result will fit in str before doing this.
This is why the result is different.

To see for your self:
******************************

char str[12]="some__string";

But make this

char str[128]="some__string";

and you're fine as far as space goes.
char append[12]="same__string";
char *next=str+(sizeof(char)*(sizeof(str)+sizeof(append)));
*next='Z';
printf("before copy %c\n",*next);
sprintf(str, "%s%s", str, append);

But this is still a bug.
printf("after copy %c\n",*next);

******************************
I didn't chk the result on gcc.

Hope this helps.

Lawrence
 
F

Flash Gordon

hongky said:
#include <stdio.h>
#include <string.h>
char str[128];
char append[128];
sprintf(str, "%s%s", str, append);
is it standard use?
i test it in VC & GCC.
the result is different, so weird! who can tell me the reason.

There's a logical error.

str is of 128 and so is 'append'. When you append 'append' to 'str' it
will write to some memory locations beyond 'str' and corrupt them. The
overwriting can 0 to 128 locations, 0 when str is empty first element
is '\0' and 128 when last element is '\0'.

This is why the result is different.

No, that is *not* the reason the result is different. The reason it is
different is because it is copying between objects that overlap,
specifically it is copying str over itself. This is undefined behaviour
even if both str and append contain 2 character strings.
To see for your self:
******************************

char str[12]="some__string";
char append[12]="same__string";

Did you realise that neither of these arrays contain '\0' terminated
strings? You need 13 byte long arrays if you want to fit "some__string"
in to it.
char *next=str+(sizeof(char)*(sizeof(str)+sizeof(append)));

sizeof(char) is guaranteed to be 1, so all you are doing is making this
harder to read.

char *next=str+(sizeof(str)+sizeof(append));

However, this still invokes undefined behaviour since you are creating a
pointer that is more than 1 past the end of str.

To be honest, I can't even see what you were trying to demonstrate with
this code, it is so badly written.
*next='Z';

More undefined behaviour, writing to a byte off the end of the array you
started the pointer from.
printf("before copy %c\n",*next);
sprintf(str, "%s%s", str, append);

This is even worse than the OPs code since str and append do not contain
strings since there was no null termination.
printf("after copy %c\n",*next);

******************************
I didn't chk the result on gcc.
Hope this helps.

I hope that no one thinks the code you posted helps since it was far
worse than what the OP posted. At least the OP only got one thing wrong.

I seriously suggest that you would be better off reading a good C text
book, such as K&R2, reading the FAQ for comp.lang.c (which I'm sure says
what K&R2 is) and asking questions about what you don't understand,
rather than posting incorrect advice.
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top