Deutsch English Français Italiano |
<v4es5k$29e2g$3@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Thu, 13 Jun 2024 15:28:20 +0200 Organization: A noiseless patient Spider Lines: 42 Message-ID: <v4es5k$29e2g$3@dont-email.me> References: <666a095a$0$952$882e4bbb@reader.netnews.com> <v4d4hm$1rjc5$1@dont-email.me> <8734ph7qe5.fsf@nosuchdomain.example.com> <v4ddvg$1ta1b$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 13 Jun 2024 15:28:21 +0200 (CEST) Injection-Info: dont-email.me; posting-host="a4019a0b35be2744ae8acc392e7d37ca"; logging-data="2406480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FiHB0PXmcW49OU3dS1mlH2mcaFnq2jHk=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:Q30t22wSd7xut7d73XWJdS4pkwg= In-Reply-To: <v4ddvg$1ta1b$1@dont-email.me> Content-Language: en-GB Bytes: 2821 On 13/06/2024 02:19, Janis Papanagnou wrote: > On 13.06.2024 00:22, Keith Thompson wrote: >> >> This: >> char outliers[100] = ""; >> initializes all 100 elements to zero. So does this: >> char outliers[100] = { '\0' }; >> Any elements or members not specified in an initializer are set to zero. > > Oops! This surprised me. (But you are right.) The overhead isn't > [syntactically] obvious, but I'm anyway always setting a single > '\0' character if I want to store strings in a 'char[]' and have > it initialized to an empty string (like below). > >> If you want to set an array's 0th element to 0 and not waste time >> initializing the rest, you can assign it separately: >> char outliers[100]; >> outliers[0] = '\0'; >> or >> char outliers[100]; >> strcpy(outliers, ""); >> though the overhead of the function call is likely to outweigh the >> cost of initializing the array. > > It wouldn't occur to me to use the strcpy() function, but is the > function call really that expensive in C ? > That depends on your toolchain. If you are using a Windows-based compiler with an external DLL for the C library and the compiler doesn't handle the strcpy() directly, then it can be quite a lot of overhead. You have the call to the DLL, which involves a few steps of indirection. The library strcpy() may be optimised for handling large strings, and may save and restore a lot of registers (such as SIMD vector registers). If you are using a compiler (whatever the platform) that optimises "strcpy", it will generate identical code to "outliers[0] = '\0';".