[Toybox] Towards find cleanup

Rob Landley rob at landley.net
Thu Apr 11 15:13:05 PDT 2013


On 04/11/2013 04:17:59 PM, Felix Janda wrote:
> On 04/10/13 at 07:34pm, Rob Landley wrote:
> snip
> > > I think that some function parameters should be made const and  
> that
> > > the code could be made less repetitive.
> >
> > I specify "static" because it allows the compiler to produce better
> > code, but I've never found "const" to actually produce better code.  
> The
> > compiler's optimizer does liveness analysis, it can tell when you  
> don't
> > actually change a value after it's assigned to. In theory const lets
> > the compiler do some of this across translation units, but that's  
> what
> > link time optimization is for these days.
> >
> > In addition, const's syntax is unfortunately subtle when pointers  
> are
> > involved. Is the pointer what's const, is what it points to that's
> > const (it binds left except at the left edge...), how about a  
> pointer
> > to a pointer, or pointer to an array...
> >
> > I also haven't seen it interface well with the actual read-only
> > sections ELF can produce. There's an -mebedded-data compiler flag  
> that
> > says to put data in the .rodata section. The main use of .rodata is  
> to
> > strings constants in there by default. Yet those strings aren't
> > "const", you can assign a string constant to a regular char *  
> without
> > the compiler ever complaining. (And not being able to do so would
> > pretty fundamentally break C.)
> >
> > What const really seems like to me is a leakage from C++'s "public,
> > private, protected" into C. It's a language facility that assumes
> > programmers can't trust themselves or their successors to get it  
> right.
> 
> I'd thought telling the compiler stuff can't hurt. Thanks for the
> detailed explainations.

It doesn't exactly hurt, but it's extra verbiage that usually has no  
compelling reason to be there, and it tends to spread. (If you pass a  
const value to a non-const function parameter, you have to add the  
const annotation to that function and so on down the call chain, and  
this can get fairly intrusive just to get the compiler to shut up.)

Just because C has a feature doesn't mean it's a good thing. We could  
annotate every local variable with the "auto" keyword if we wanted to.  
There's just no point. Yes "const" does more than "auto", but not  
_enough_ more to convince me it's worth it. But I'm open to  
counterarguments if somebody cares more strongly about it than I do...

(I've seen some uses of static const int as alternatives to #defines or  
enums, where people want type safety. I've then seen people typecast  
them. *shrug*)

Rob
 1365718385.0


More information about the Toybox mailing list