Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't -O0 disable gcc compile optimization?

Tags:

c++

string

g++

string str="fujian";

Some books say that code will trigger the copy constructor, but g++ will optimize it so that the copy constructor won't be called.

However, I used g++ command -O0 to disable the optimization, but it still can't trigger the copy constructor.

How to understand it?

like image 248
jiafu Avatar asked Apr 27 '13 14:04

jiafu


People also ask

How do I keep my compiler from optimizing variables?

Select the compiler. Under the Optimization category change optimization to Zero. When done debugging you can uncheck Override Build Options for the file. In the latter case the volatile defined inside the function can get optimized out quite often. ...

How do I disable compiler optimization GCC?

Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command-line options.

How do I disable GCC?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

Is GCC an optimizing compiler?

GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O , this option increases both compilation time and the performance of the generated code.


2 Answers

With GCC and Clang you can use the -fno-elide-constructors compilation flag to turn off copy/move elision optimization.

like image 145
Andy Prowl Avatar answered Nov 14 '22 23:11

Andy Prowl


The copy elision rule is based on ISO C++ 12.8. While other rules used generally for optimization are collectively called as "as-if" rule in clause 1 (which allows the implementation generates program behave somewhat differently with the "non-optimized" program semantics based on abstract machine model), this rule is so special that you can treat the "optimized" code itself behaves as exact as the original meaning. In other words, the elided constructor calls may not exist at all in the abstract machine's behavior.

If there is no undefined behavior, with or without optimization according to the as-if rules, the observable behavior of the optimized program and non-optimized program should be same (although they may differ on performance, etc). However, copy elision is more aggressive, namely, it can alter the observable behavior.

You'd better not rely on the differences produced by copy elision. Thus, it is reasonable to keep same behavior for ordinary optimization options and provide a separate option to control the precise (different) behavior for users who know the risks well and indeed need it.

WG21/N4296

1.9 Program execution

5 A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

8 The least requirements on a conforming implementation are:

(8.1) — Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

(8.2) — At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.

(8.3) — The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.

These collectively are referred to as the observable behavior of the program. [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. —end note ]

12.8 Copying and moving class objects

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

...

like image 45
FrankHB Avatar answered Nov 14 '22 23:11

FrankHB