[vox] things that really suck about C!

Harold Lee harold3 at gmail.com
Sun Feb 28 23:20:18 PST 2010


On Sun, Feb 28, 2010 at 10:49 PM, Carl Boettiger <cboettig at gmail.com> wrote:
>> One calls a function and the arguments are passed by value. Call a
>> function with an array as an argument, and feel free to modify its
>> contents!
>>
> so declaring an array as const prevents this, func(const double * a).  I
> understand that this also helps the compiler make optimizations it cannot do
> when you don't use const.  I think you could still modify the contents of
> the array by first copying the pointer though,
>
> double * b = a;
> b[i] = something new.
>
> So there's also the modifier "restrict", which I believe would prevent this,
> and again helps out the compiler do smart things.  Others can probably
> confirm/correct this?  Is it good practice to use these modifiers as often
> as possible/appropriate?
>

Yes, using const as much as possible really helps. When I see const
used correctly and as often as possible, it makes me more confident
that the functions I'm calling are well written.

Google's internal C++ style guidelines agree:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Use_of_const

I've worked on projects before where others didn't agree, and they'd
never mark parameters as const in any way. Then my functions that call
theirs can't mark parameters as const, and so on, up the call chain.
In C++ world, the same problem happens when methods aren't marked as
const when they should be.

Harold


More information about the vox mailing list