[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
On Sun, Aug 27, 2000 at 06:28:48PM +0100, Laurence Withers wrote:
> In reply to George Foot (George Foot <gfoot@xxxxxxxxxx>):
> >Casting away constness is very bad though -- the compiler can make
> >optimisations assuming the pointed-to data does not change, when in fact
> >you're altering it. Acquiring the surface is changing the data pointed
> >to (several levels of indirection further down), so the function should
> >not take a const parameter.
>
> Mark changeable data as `volatile' - this works as a poor man's
> `mutable' C++ keyword. In essence, you are telling the compiler it can
> never trust the contents of this keyword to be the same after any time
> has elapsed (ie. the code a = sqrt(b); c = sqrt(b); may not give a,c
> such that a == c).
>
> And so the compiler does not make the assumption that this bit of data
> will be constant.
But the contents of the struct don't normally change behind the
scenes -- they only change in these specific functions. When
you acquire a bitmap in Windows, it sets the bitmap's `locked'
flag. It may also rewrite the `line' array with different
addresses. This doesn't happen magically, it happens
specifically when you try to acquire a bitmap, and any access to
the bitmap must acquire it first (if it's not already acquired).
It would really hurt performance if a loop using some element of
the BITMAP struct had to reread that element each time it used
it, even if the compiler hasn't changed it and no functions have
been called in the meantime. To use your example, the code:
a = b*b; c = b*b;
would result in four reads of b, two multiplications, and two
writes, if b were volatile.
> The const keyword isn't just the simple statement "compiler, I will not
> change this object's data" - it is the statement "user, I will not
> change your object's appearance or behaviour".
I disagree -- if it's a message for the user, you should write
/*const*/. The keyword `const' has meaning to the compiler, and
using it properly lets the compiler perform better
optimisations. But if you overuse it, the generated code may be
wrong.
George
--
Random project update:
09/05/2000: Libnet 0.10.8 uploaded -- a few bugfixes
http://www.canvaslink.com/libnet/ (try changes-0.10.8.txt)