[Top][All Lists]

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

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

From: Larry McVoy
Subject: Re: C Strings and String Literals. (Was: Pascal rides again)
Date: Sun, 13 Nov 2022 13:41:19 -0800
User-agent: Mutt/1.5.24 (2015-08-30)

On Sun, Nov 13, 2022 at 10:25:35PM +0100, Alejandro Colomar wrote:
> 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...

I'm not going to argue the point, I'm retired and just don't care that much.

I will say a few things:

A) I have decades of still running code that shows it is entirely possible
   to write complex code in C that works just fine.

B) I get it, there are many ways you can shoot yourself in the foot.  That's
   the lack of guard rails.

C) I suspect there are some options to gcc that will catch most, if not all,
   of the foot shootings.

D) This reminds a little of a radio conversation I heard on the ocean a 
   month ago while chasing bluefin tuna (*).  We were fishing up mackerels
   for bait, they coming brown (spanish) and the more flashy green backs.
   There is the usual radio chatter about "where are the macks" and a guy
   gives the location and says he has a dozen spanish macks in the livewell.
   "No green backs?  I really want greenbacks"
   "The brown ones work"
   "Nowhere near as well as greenbacks"
   "How many tuna have you caught on greenbacks"
   "Well, we're still looking for that first one"
   "LOL.  I've caught 8 bluefin last year and this year and every one
   came on Spanish mackerel"

(*) We got 'em. 140 pound big eye and
    23 20-30 pound albacore.  Then a 70 pound bluefin in our local waters.
    Banner year.  And now you know why I don't care about tech :-)

reply via email to

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