This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Incompatible behavior -O0, -O3, std::cout


Thanks Andrew. That answers it.

Pankaj


On Fri, 18 Nov 2005, Andrew Pinski wrote:



Hello Everyone


I noticed some thing strange recently. This code (under g++ (GCC) 3.2.3
20030502 (Red Hat Linux 3.2.3-53)), provides this output with -O0 flag:
int main(int argc, char **argv) {
  std::cout << f1() << f2() << std::endl;
}


I'm pretty sure that I am depending on an undefined behavior here, but maybe you guys would want to have a deeper look at this.


Yes are you dependening on undefined behavior.   Since there is no sequence point
between the calls to f1() and f2(), then they can be evaluated in either order.

I think, if are global, it would be same as depending upon the order of
evaluation of arguments to a function (which would be wrong according to
the C++ standard), but if they were members of the stream classes, then it
would evaluate to a().b().c().d() and we should expect f1() to be called
before f2(). - Is that correct?

No because the above is equvliant to the following pesdu C code:


operator <<(operator << (operator << (std::cout, f1()), f2()), std::endl)

and the order evaluatation of expressions inside a function call is undefined.

Thanks,
Andrew Pinski



--




--------------------------------------------------
 Pankaj Gupta
 Infrastructure Team   -   Tower Research Capital

 Phone: 212-219-6012 [Work]
        551-358-0684 [Cell]

 Mail:  pankaj.gupta@tower-research.com
--------------------------------------------------



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]