Hi Edmund,

I use constants at every opportunity.   I discovered long ago
that constants help even when they are not part of a prototype.

For instance in my program the boardsize is a constant, so there
must be a different version for each boardsize.  

- Don


On Sat, 2006-12-23 at 12:01 +0000, Edmund Stephen-Smith wrote:
> I've never encountered an example where using const could slow a
> program down.  Using const properly in C gives the compiler
> opportunities to optimise.  For example consider the crude
> construction:
> 
> void
> scan_array(char *array)
> {
>       char *p;
>       for (p= array; *p != 0; p++)  {
>               if (*p == 'a')
>                       do_something(p, 1);
>               if (*p == 'b')
>                       do_something(p, 2);
>       }
> }
> 
> The declaration of do_something() affects how the compiler can
> optimise.  Let's say the compiler has put both p and *p in registers.
> If do_something() is declared as:
> 
> void  do_something(const char *, int);
> 
> then *p is still valid after the function call.  However if
> do_something() is declared as:
> 
> void  do_something(char *, int);              /* NB: no "const" */
> 
> then there's the possibility that do_something() might have changed
> the value in the array (e.g. from 'a' to 'b').  So, the compiler
> *must* reload the value from the array again -- another memory
> reference.
> 
> Edmund.
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] Behalf Of Don Dailey
> > Sent: 19 December 2006 13:13
> > To: House, Jason J.
> > Cc: computer-go
> > Subject: RE: [computer-go] Fast Board implementation
> >
> >
> > On Tue, 2006-12-19 at 02:21 -0500, House, Jason J. wrote:
> > > The const keyword has been completely removed from the
> > language and
> > > makes it hard to force such restrictions on "library users".
> >
> > I use const all the time in my C code but only for myself -
> > almost like
> > a comment in the source code saying "don't mess with this pointer."
> >
> > I thought all it did was generate compiler warning messages,  but I
> > never payed that much attention to it.    I just automatically stuck
> > it the prototype to indicate to me it was read only.
> >
> > In fact, I will probably take it out if it causes the compiler to
> > generate extra code.   I would think such a think might help the
> > compiler optimize though.
> >
> > - Don
> >
> >
> > _______________________________________________
> > computer-go mailing list
> > computer-go@computer-go.org
> > http://www.computer-go.org/mailman/listinfo/computer-go/
> 

_______________________________________________
computer-go mailing list
computer-go@computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/

Reply via email to