Re: [AD] pack_fwrite and const

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


In reply to Vincent Penquerc'h <vincent@xxxxxxxxxx>:
>(what use is const if you cast it away as soon as you 
>get a const ptr ?)

If a function's parameter is declared `const', then the compiler can
assume certain optimisations. A trivial (C++) example:

// begin

#include <iostream>

void do_some_stuff(const int& a);

int main()
{
int x = 7;
do_some_stuff(x);
cout << x << endl;
}

// end

In this case, the compiler knows that the value of `x' remains constant
at 7 throughout main(), so it doesn't need to store `x' anywhere; it
just generates assembly instructions with a constant. This is obviously
quicker.

As to your patch, it would be good to commit this. There is no reason to
cast away the `const' qualifier, and doing so is a bad habit because use
of the qualifier provides optimised code and allows the compiler to
check for accidental writes. For instance, in the pack_fwrite()
function, consider the expression:

   *(p++)

This reads the value at address p, and then increments p by one. But if
it was accidentally written as:

   (*p)++

then we would increment the value at address p, and store it there. This
is an obvious error, and the compiler would catch it for us.

Bye for now,
-- 
Laurence Withers, lwithers@xxxxxxxxxx
                http://www.lwithers.demon.co.uk/

Attachment: signature.asc
Description: PGP signature



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/