API : constness ?
Erik Max Francis
max at alcyone.com
Tue Jun 1 14:42:03 EDT 2004
Andrea Griffini wrote:
> You can declare a constant pointer (you can't change
> the pointer) to constant data (you can't change the
> data). But still those properties are properties of
> the pointer, not of the data. I know it may be
> surprising at a first sight, but the code generated
> by the compiler is not allowed to assume that the
> "pointed to" data will not change; the reason is
> that the limit of "you can't change the data" is
> indeed just related to the pointer... in other words
> the limit is only that you can't change the data
>
> ==> USING THAT POINTER <==
>
> By no means you're stating the data is really constant.
You're eliminating an important distinction here, which is the
difference between a pointer-to-const parameter, and data that is
actually const. You're talking about a pointer-to-const parameter, I'm
talking about data that's actually const. The original poster wrote
code that created data that was actually const, and then passed it to a
function that he wanted to take a pointer-to-const parameter.
> Note also that casting away const-ness from a pointer
> and changing the data is something that must be
> supported if the data is not really constant.
Yes, that is correct.
> In other words:
>
> void foo(const int *x)
> {
> int *y = (int *)x;
> ++(*y);
> }
>
> int main()
> {
> static int a = 3;
> foo(&a);
> // Here a will be 4
> ...
> }
>
> The above code is perfectly legal; looking at main()
> and at the declaration of foo the compiler cannot
> decide to put "a" in read-only memory.
Yes. And if a were declared
static const int a = 3;
it would be illegal. That is the case that I was discussing.
--
__ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ But who shall dwell in these worlds if they be inhabited?
-- Johannes Kepler
More information about the Python-list
mailing list