strings and constness

Rob Landley rob at landley.net
Sat Jun 17 23:55:16 UTC 2006


On Tuesday 13 June 2006 11:19 pm, Erik Hovland wrote:
> On Tue, Jun 13, 2006 at 11:55:27AM -0500, Jason Schoon wrote:
> > On 6/13/06, Erik Hovland <erik at hovland.org> wrote:
> > >On Tue, Jun 13, 2006 at 09:28:47AM -0500, Jason Schoon wrote:
> > >> --- busybox/networking/tftp.c 2006-06-13 09:26:45.000000000 -0500
> > >> +++ busybox/networking/tftp.c.mine    2006-06-13
> > >> 09:26:11.000000000-0500 @@ -95,7 +95,7 @@
> > >>       return blocksize;
> > >>  }
> > >>
> > >> -static char *tftp_option_get(char *buf, int len, const char const
> > >
> > >*option)
> > >
> > >> +static char *tftp_option_get(char *buf, int len, char const *option)
> > >
> > >You could write this as:
> > >static char *tftp_option_get(char *buf, int len, const char *const
> > > option)
> > >
> > >The const in the right place would allow the compiler to further
> > >optimize the case where both the dereferenced string and the pointer to
> > >the string are not changed in the function that they are being passed
> > >to. Not sure if that adds to a real space savings.
> > >
> > >See my post and patch to utils.c and utils.h where I did this for
> > >another function:
> > >http://busybox.net/lists/busybox/2006-June/022287.html
> >
> > I see that now, I thought it was a typo since my compiler threw a warning
> > about it.  If there are valid space savings for it, we need to find a way
> > to suppress the warning.
>
> If you want to make the string const, put the const before the *.

In build-at-once mode the compiler _really_ should be able to figure this 
out...

Rob
-- 
Never bet against the cheap plastic solution.



More information about the busybox mailing list