Re: faster access private or public?

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 09/28/04


Date: Tue, 28 Sep 2004 10:46:19 -0400


"Sigurd Stenersen" <sigurds@utvikling.com> wrote in message
news:OcBuZ6UpEHA.744@TK2MSFTNGP10.phx.gbl
> I guess I have to spell it out...
>
> void g(int i)
> {
> OhFooey->~Foo();
> new (OhFooey) Foo(10);
> }
>
> In this case it *is not* okay for the compiler to assume that private
> variables are not changed when optimizing some other piece of code
> that uses the instance pointed to by OhFooey and that code calls g().
> I believe we all agree on that ?

Right. But this code has well-defined behavior, so I don't quite see how
it is relevant to the question at hand.

> Now, what you're saying seems to be that in this case...
> void g(int i)
> {
> new (OhFooey) Foo(10);
> }
> ...it *is* okay for the compiler to make that same assumption.

Only if the compiler can somehow prove that this code is always called
in a context where it causes undefined behavior. It would take a very
smart compiler to do that, though. You cannot say that just looking at
the code of g() in isolation, since it can be used legally, as in:

OhFooey->~Foo();
g(1);

> I don't understand why you think this is OK, but anyway could you
> tell me how the compiler is going to know what is actually being done
> in this part of the code while optimizing some other piece of code ?

It does not matter what the compiler knows or does not know, or what
reasoning it employs or does not employ. The moment a code that exhibits
undefined behavior is executed, you cannot argue any longer whether the
compiler-generated code is right or wrong. By definition of undefined
behavior, anything the program might do from that point on is correct.
You cannot argue that the code is wrong, because to argue that, you have
to say: I expected the code to behave in such and such a way, but I
observe it behaving differently, therefore it must be wrong. By coding
undefined behavior, you lose the right to claim any expectations as to
the compiler output, so you cannot clear the first hurdle of that
two-pronged test (yes, I had to read a lot of legal documents lately).

-- 
With best wishes,
    Igor Tandetnik
"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


Relevant Pages

  • Re: how to release memory?
    ... interest is that a pointer p is indeterminate. ... except that the compiler is free to optimize the statement away ... Is 'expr;' really invoking undefined behavior if expr's value ... standard doesn't ever say you cannot dereference a void*, ...
    (comp.lang.c)
  • Re: const_cast<>
    ... required to issue a diagnostic message for "void main". ... There is no point in asking why a particular compiler produces any ... particular results once you invoke undefined behavior. ...
    (comp.lang.cpp)
  • Re: Function with unspecified number of arguments
    ... void f ... The function body above defines a function that accepts NO ARGUMENTS ... undefined behavior. ... All you did was tell the compiler not to check that you called f ...
    (comp.lang.c)
  • Re: "Sorting" assignment
    ... too slow, other times for optimizing too much, some times for being too ... That is a rather bizarre, not-quite-C compiler, but ok. ... The overlap issue is a different problem, but performing a "swap" on ... CPU hardware feature differences that can be incredibly important to ...
    (comp.programming)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)