[Bf-committers] Casting arrays (matrices)

Ton Roosendaal bf-committers@blender.org
Thu, 3 Jul 2003 00:10:41 +0200


Hi,

So much passion over such a minor topic! :-)

Of course I understand the advantage of a 'const char' string. I am  
talking about something else here. Read this prototype for a function:

void Mat3Invert(float mat1[][3], const float mat2[][3])

Now here, some purist coder thought to be helpful. Instead he made an  
error. The second matrix is not 'const' itself, but only the mat2  
POINTER is! Well, is that interesting to know!

I don't know why it gives warnings. Doesn't interest me. If you want to  
add information to these calls, use a nice /* comment */

And don't reply to this without checking every texbook you have on C!

-Ton-


On Wednesday, Jul 2, 2003, at 23:01 Europe/Amsterdam, Douglas Toltzman  
wrote:

> I'm with Maarten on this one.  If I gave you a prototype for a string  
> copy
> function that read "strcyp(char *target,const char *source)" it would  
> not
> only tell you that your source string wouldn't be modified, but it  
> would
> allow the compiler to enforce that behavior.
>
> The "const" modifier is a great documentation feature, at the very
> minimum, and provides an additional level of protection from coding  
> errors
> at best.  Incidentally, it also helps the optimizer.
>
> On Wed, 2 Jul 2003, Maarten Gribnau wrote:
>
>> Hi Ton
>>
>>> Reading the arithb.c code, I found that someone (NaN) has included a
>>> lot of 'const' variables in the function declarations. I have
>>> absolutely no clue what it's good for. Originally, the code worked
>>> fine without, as still is in the MTC_matrixops.c version in
>>> source/blender/blenlib/
>>>
>>> I've removed the 'const' everywhere from arithb.c. The code compiles
>>> quite a lot faster, with only a few and more useful warnings left.
>> How does the const cause trouble?
>> The const is actually useful. It tells you the function is not going  
>> to
>> modify the variable (usually pointers and references in C++) passed to
>> it. It makes the variable an "in only" variable.
>>
>> Maarten
>
> _______________________________________________
> Bf-committers mailing list
> Bf-committers@blender.org
> http://www.blender.org/mailman/listinfo/bf-committers
>
>
------------------------------------------------------------------------ 
--
Ton Roosendaal  Blender Foundation ton@blender.org  
http://www.blender.org