Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Why does C allow structs to have a tag? Date: Wed, 11 Aug 2021 11:41:23 -0700 Organization: None to speak of Lines: 34 Message-ID: <87lf57j030.fsf@nosuchdomain.example.com> References: <87czqnlp2m.fsf@bsb.me.uk> <5d8700fd-6ba7-4c72-ae2f-1d4ea91fe823n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="7af0248d926c60a6cf88b66f75a520dd"; logging-data="11204"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ahdFo1xlqc1YG//ptsJUJ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:vAVn0xuxbiLaOCRJ5cJRjf/NAzE= sha1:Gve5mJ7KhTh8SCjIQbT2A5h39/M= Xref: csiph.com comp.lang.c:162329 scott@slp53.sl.home (Scott Lurndal) writes: > Malcolm McLean writes: >>On Wednesday, 11 August 2021 at 14:52:54 UTC+1, Scott Lurndal wrote: >>> >>> strcat is almost never the right interface to use. It needs >>> to scan the entire string to find the terminal nul-byte before >>> appending. As the string gets longer, each strcat takes longer >>> than the one before. >>> >>> Just keep a pointer to the next element and increment the >>> pointer each time you add a new element. >>> >>Most strings are short. Building up a url with strcat is not going to >>stress a 3GHz machine. If the strings become huge then, yes, you >>need to look at more efficient ways of concatenating them. > > Building it with snprintf() will be far more flexible and efficient. > > Every cycle matters. As Bart's measurements indicate, snprintf() is likely to have substantial overhead, probably because it has to parse its format string. I wouldn't assume snprintf() is going to be faster than strcat() without measuring it for the specific data I'm working with. The non-standard strlcat() returns the initial length of the destination plus the length of the source, which can be used to find the end of the copyied string without re-scanning. (It can be a little tricky to get the compilation command right on some systems.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */