Some question about one method.
Hi guys,
I'm have a some question
how I can change the:sprintf(tmp,"%s%c",tmp,sym);
where - char *tmp;int sym;
may I use strcat or another method.
I need a fast and better method for do it.
Thanx.
Signature · 3 lines
Senior Developer of Crew IT research labs Alphex Kaanoken <[email protected]> Web: http://crew.org.ru
On Fri, 2004-05-21 14:24:41 +0400, Alphex Kaanoken <[email protected]> wrote in message <[email protected]>:
Quoted 7 lines
Hi guys,
I'm have a some question
how I can change the:sprintf(tmp,"%s%c",tmp,sym);
where - char *tmp;int sym;
may I use strcat or another method.
I need a fast and better method for do it.
That's perfectly good in C. Why do you want to change that? It the length of ths ASCIIZ string pointed to by temp is known, there's a good chance to optimize it. However, that's against readability, so I'd advise against it.
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
On Fri, 2004-05-21 14:24:41 +0400, Alphex Kaanoken <[email protected]> wrote in message <[email protected]>:
Quoted 7 lines
Hi guys,
I'm have a some question
how I can change the:sprintf(tmp,"%s%c",tmp,sym);
where - char *tmp;int sym;
may I use strcat or another method.
I need a fast and better method for do it.
Jan-Benedict Glaw commented:
Quoted 4 lines
That's perfectly good in C. Why do you want to change that? It the length of ths ASCIIZ string pointed to by temp is known, there's a good chance to optimize it. However, that's against readability, so I'd advise against it...
I must disagree : this piece of code is NOT perfectly good in C.
1) sprintf is a nest of buffer overflow bugs waiting to bite. The issue is of course the size of the tmp buffer. If it is known, the code can be optimized and that will not make it less readable, conversely, if it is not known, then we have a real bug in front of us, as extending the string by one character will likely overwrite whatever is next in memory. Don't use sprintf ! use snprintf. If you cannot use snprintf, then you are in trouble.
2) assuming you can use the destination buffer as an argument to sprintf is asking for trouble ! Obviously it would almost certainly fail if not used as the first argument for a format string starting with %s : a very restrictive case. ISO-C is very clear about such an abuse: 7.19.6.6 Description "...If copying takes place between objects that overlap, the behavior is undefined." A C library that would not support it and modify tmp before it is used as the source for %s would not be incorrect. The unsuspecting C newbie (and the vast majority of his elders) would not understand why sprintf(tmp,"%s%c",tmp,sym); would "work" and sprintf(tmp,"%c%s",sym,tmp); would "fail".
Do not encourage such ugly code.
If you know tmp will accomodate the extra character, why not use the following code:
char *p = tmp + strlen(tmp);
p[0] = sym;
p[1] = '\0';
Or better, if you know tmp size:
int len = strlen(tmp);
if (len + 1 < tmp_size) {
tmp[len++] = sym;
tmp[len] = '\0';
}// notice how len is still the length of the tmp string.
Lastly, if you do not understand the above code snipplets, you are lacking some fundamental C concepts esp. pointers and should not undertake any significant programming project in C. This is just friendly advice, as debugging it will be horribly frustrating.
JBG's concern about readability would be best addressed with a utility function, possibly implemented inline such as:
char *strncat1(char *buf, int bufsize, int c);
Chqrlie.
PS: a few simple trick questions for C newbies :
- what is -1 ?
- what is wrong with : enum BOOL { FALSE=0, TRUE=1 };
- what is the difference between : i << 2 and i * 4
- same question for : i >> 2 and i / 4
On Mon, 2004-05-24 10:06:51 +0200, Charlie Gordon <[email protected]> wrote in message <[email protected]>:
Quoted 18 lines
On Fri, 2004-05-21 14:24:41 +0400, Alphex Kaanoken <[email protected]> wrote in message <[email protected]>:
Quoted 7 lines
Hi guys,
I'm have a some question
how I can change the:sprintf(tmp,"%s%c",tmp,sym);
where - char *tmp;int sym;
may I use strcat or another method.
I need a fast and better method for do it.Jan-Benedict Glaw commented:
Quoted 4 lines
That's perfectly good in C. Why do you want to change that? It the length of ths ASCIIZ string pointed to by temp is known, there's a good chance to optimize it. However, that's against readability, so I'd advise against it...
I must disagree : this piece of code is NOT perfectly good in C.
Quoted 7 lines
1) sprintf is a nest of buffer overflow bugs waiting to bite. The issue is of course the size of the tmp buffer. If it is known, the code can be optimized and that will not make it less readable, conversely, if it is not known, then we have a real bug in front of us, as extending the string by one character will likely overwrite whatever is next in memory. Don't use sprintf ! use snprintf. If you cannot use snprintf, then you are in trouble.
There are sane uses of sprintf(). C is (and always was) by experts for experts. So you've to do proper allocation anyways:)
Quoted 3 lines
2) assuming you can use the destination buffer as an argument to sprintf is asking for trouble ! Obviously it would almost certainly fail if not used as the first argument for a format string starting with %s : a very
Argh, you're right on this one. I didn't catch it :-(
Quoted 8 lines
restrictive case. ISO-C is very clear about such an abuse: 7.19.6.6 Description "...If copying takes place between objects that overlap, the behavior is undefined." A C library that would not support it and modify tmp before it is used as the source for %s would not be incorrect. The unsuspecting C newbie (and the vast majority of his elders) would not understand why
sprintf(tmp,"%s%c",tmp,sym); would "work" andsprintf(tmp,"%c%s",sym,tmp); would "fail".
Do not encourage such ugly code.
I don't - I'm totally against it now that I've seen that. I never ever came to the idea of using a source string for destination...
[code fragments]
Quoted 4 lines
Lastly, if you do not understand the above code snipplets, you are lacking some fundamental C concepts esp. pointers and should not undertake any significant programming project in C. This is just friendly advice, as debugging it will be horribly frustrating.
I second that. C's syntax is quite simple, but only people who really understand C's pointer and allocation concepts should use it.
Quoted 4 lines
JBG's concern about readability would be best addressed with a utility function, possibly implemented inline such as:
char *strncat1(char *buf, int bufsize, int c);
Right:)
Quoted 4 lines
- what is -1 ?
- what is wrong with : enum BOOL { FALSE=0, TRUE=1 };
- what is the difference between : i << 2 and i * 4
- same question for : i >> 2 and i / 4
That's mostly sign problems/questions. (I don't think there's something technically wrong with the enum BOOL, but you shouldn't use it except with other enum BOOL xxx variables ...). But more interestingly:
char *a;
short *b;
int *c;
long *d;
long long *e;
--> What's the difference between a++ and e++?
struct {
char first;
char second;
} s1;
struct {
char both[2];
} s2;What's the difference:)
But to come to an end, I haven't seen the abous of tmp in the given example...
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Hi JBG,
Quoted 1 line
There are sane uses of
sprintf()...
There is almost no overhead to using snprintf. And it is so difficult to prove that a given sprintf format cannot overflow... Just try this one:
what if ints are 64 bits now, is the buffer large enough to accomodate all these digits ?
Yet there is much worse function : strncpy. This sucker does NOT do what you think it does. Pull the man page, read it, read it again... unbelievable isn't it ? Test it, grep any source files for this buddy, I bet you'll find subtle bugs stuck to it like fly paper.
Quoted 3 lines
C is (and always was) by experts for experts.
C's syntax is quite simple, but only people who really understand C's pointer and allocation concepts should use it.
I agree completely on this, programming in C is like riding down the guardrail, except it's more like a razor blade.
It's just so hard to always keep a sharp eye for all the pitfalls one comes across in even the simplest one liners.
I urge you to get a look at http://www.joelonsoftware.com/
I especially like his "Guerrilla Guide to Interviewing"
(http://www.joelonsoftware.com/articles/fog0000000073.html) :
"I've discovered that understanding pointers in C is not a skill, it's an aptitude. For some reason most people seem to be born without the part of the brain that understands pointers. This is an aptitude thing, not a skill thing - it requires a complex form of doubly-indirected thinking that some people just can't do."
Quoted 4 lines
Quoted 1 line
- what is wrong with : enum BOOL { FALSE=0, TRUE=1 };
(I don't think there's something technically wrong with the enum BOOL, but you shouldn't use it except with other enum BOOL xxx variables ...)
Well you are going to love this one!
defining BOOLeans TRUE and FALSE as an enum makes them available for symbolic debugging, but you should still #define them to avoid nasty surprises with the preprocessor:
#if TRUE
// if you don't, this code will never compile
#endif
Quoted 7 lines
char *a;
short *b;
int *c;
long *d;
long long *e;--> What's the difference between a++ and e++?
it makes them point to the next element in the arrays of respective types, and as such increments the binary value by a different amount.
More interestingly what is the difference between e[5] and 5[e] ?
Quoted 8 lines
struct {
char first;
char second;
} s1;
struct {
char both[2];
} s2;
what's the difference ?
On most architectures, not much. But C makes no guaranties as to the actual layout of structures.
In particular (&s1.second - &s1.first) may be different than 1. Similarly sizeof(s1) can be different from sizeof(s2).
Glad you found my quiz useful.
Chqrlie.
PS:) did you know about this new C library extension : imprecations you can pass extra information to some of the C runtime APIs, thus preventing some of the most annoying problems in C.
for example, memory allocation issues are finally solved :
p = malloc(n), "nofail";
free(p), "pack";
p = realloc(p, newsize), "nomove";
best of all, your compiler most likely already supports imprecations ;-)
Quoted 11 lines · Charlie Gordon
Quoted 7 lines
char *a;
short *b;
int *c;
long *d;
long long *e;--> What's the difference between a++ and e++?
it makes them point to the next element in the arrays of respective types, and as such increments the binary value by a different amount.
More interestingly what is the difference between e[5] and 5[e] ?
Since pointer addition is commutative [i.e., *(e+5) == *(5+e) ] they're the same thing.
My favorite C obfuscation. (And I only use it when I intend to be obtuse! :) Note below that "j" is an int and "t" is an array of char and there's a "putchar(j[t])" in there. [Yes, it compiles and runs.]
--
int main(void){int j=2003;/*(c)2003 cishikawa.*/ char t[]="+<WJ> @abcdefghijklmnopqrstuvwxyz.,\n\""; char*i="y>b<+x,[email protected]@dt<Wupi\"JWhysbJirviiy"; while(*i)((j+=strchr(t,*i++)-(int)t),(j%=sizeof t-1), (putchar(j[t])));return 0;}/* under GPL */
Quoted 9 lines · Charlie Gordon
Quoted 8 lines
struct {
char first;
char second;
} s1;
struct {
char both[2];
} s2;
what's the difference ?
Quoted 3 lines
On most architectures, not much. But C makes no guaranties as to the actual layout of structures.
In particular (&s1.second - &s1.first) may be different than 1.
More generally, (&s1.second - &s1.first) is meaningless. Pointer arithmetic is only defined when both pointers refer to elements of a common array.
Signature · 1 line
Glynn Clements <[email protected]>