473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

about sprintf function

#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: ho****@hotmail.com
Nov 14 '05 #1
6 1839
hongky gump wrote:
#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

Nov 14 '05 #2
hongky gump <ho****@hotmail.com> wrote:
#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".

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #3

"hongky gump" <ho****@hotmail.com> wrote in message
news:cp***********@mail.cn99.com...
#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.
Nov 14 '05 #4
hongky gump wrote:
#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.

Nov 14 '05 #5
On Tue, 14 Dec 2004 05:02:55 -0800, Taran wrote:
hongky gump wrote:
#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

Nov 14 '05 #6
On 14 Dec 2004 04:30:10 -0800
"Taran" <ta************@honeywell.com> wrote:
hongky gump wrote:
#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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: TheTeapot | last post by:
Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: $arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so:...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
4
by: ypjofficial | last post by:
Hello All, To use sprintf function we have to first create a char * and assign some memory to it or we have to fixed memory sized array. eg. char str;//or it can be //char * str =(char...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
10
by: xiao | last post by:
Can anyone help me to find out what are the structure of 'DataTable' ? Does loop the 14 layers or the 800*800 array first? (NumX=NumY=800) gridde and WriteHeader are two structures Thank you~~~...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.