groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C Strings and String Literals. (Was: Pascal rides again)


From: Alejandro Colomar
Subject: Re: C Strings and String Literals. (Was: Pascal rides again)
Date: Sun, 13 Nov 2022 22:25:35 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1

Hi Larry!

On 11/13/22 21:45, Larry McVoy wrote:
> None of this surprises me but it reminds me of how I described C to
> my older son who is learning computer science.
>
> C is sort of like driving a sports car on a narrow mountain road with
> no guard rails.  If you like to text while you drive, C is not for you.
> If you can't keep your eyes on the road, C is not for you.  If you are
> a bad driver (programmer, duh), C is not for you.  But if you are an
> expert driver, like to go fast, pay attention, C is just fine, great
> in fact.
>
> Source: been a C programmer for more than 40 years, have written
> commercially used products over and over again in C.  I love C, I get
> that it is not for everyone but it is an excellent language in the hands
> of expert programmers.
Hi Larry!

I missed your reply; would have CCd you too (but you probably will get it from the list anyway). I agree most of the time, but there are still rough corners that need to be polished. See below.


On 11/13/22 22:20, Alejandro Colomar wrote:
See for example some (part of a) change that I did for optimizing some code, where I transformed pointers to char to char arrays (following Ulrich Drepper's article about libraries).  The global change using arrays instead of pointers reduced the code size in a couple of KiB, IIRC, which for cache misses might be an important thing.


-static const char *log_levels[] = {
+static const char  log_levels[][8] = {
      "alert",
      "error",
      "warn",
      "notice",
      "info",
      "debug",
  };


As a note, I used 8 for better alignment, but 7 would have been fine.  Now, let's imagine that I append the following element to the array: "messages"? Values of beta will give rise to dom!


Do I really need to load a gun and point it to my feet just to get that performance?  Wouldn't it be nice to use -Wunterminated-strings and let the compiler yell at me if I write a string literal with 8 letters?  I mean, it should be simple for a compiler to implement that, and I would be sooo much happier!


This is not at all like driving a fast car and being an excellent driver. This is like driving a fast car in that narrow mountain, and knowing that closing your eyes will make you even faster. Will you close them? :)

Did nobody in the last 40 years, or more, realize how ****ing unsafe that construct is? Is the compiler warning so hard to implement? Or maybe it exists but is not on by default and I never heard of it...

Cheers,

Alex

--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]