473,241 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

const debug variable optimized away if false?

I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";

Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?

Thanks!
Markus
Jul 23 '05 #1
11 3463
"Markus Dehmann" <ma************@gmail.com> wrote...
I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";

Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?


Nope. Nothing is guaranteed when it comes to optimisation. Unless
you remove it yourself somehow, it may still be there.

The more generically guaranteed approach is to use a macro that would
expand to nothing if NDEBUG is defined:

#ifndef NDEBUG
#define PRINTDEBUGMESSAGE(a) cerr << (a)
#else
#define PRINTDEBUGMESSAGE(a)
#endif

Now, when you use

PRINTDEBUGMESSAGE("debug message");

in your code, it will be an empty statement if NDEBUG is defined (usually
so in release builds).

Similar approach can be taken to output more than one item at a time.

V
Jul 23 '05 #2
Nope.

For instance, if you compile debugging information into your
executable, many compilers will not optimize because a lot of
optimizations (e.g. inlining) screw with the debugger.

If you want to be 100% positive it's not there, I'm pretty sure you
have to use the preprocessor. I would do something like this:

#ifdef DEBUG
#define DEBUG_PRINT(string) cerr << (string)
#elseif
#else
#define DEBUG_PRINT(string)
#endif

(Probably there are a couple subtle errors there. I don't know whether
to put the ; in the macro or not either.)

Then you just do DEBUG_PRINT("debug message");
That said, if you compile with optimizations the compiler almost
certainly will do it.

You could always look at the assembly output and see too be sure.

(There are things you could do to increase the chance nothing would be
there, but I don't think it's worth it.)

Jul 23 '05 #3
most commercial grade compilers will optimize it away. But the standard
doesn't guarantee it

Check the assembly generated and see it actually happens for your
compiler. Also most compilers dont perform optimizations by default for
a DEBUG build

Raj

Jul 23 '05 #4
Wow, I swear I didn't see your post before making mine...

(Also I noted an extranious #elseif in mine; get rid of that. That's me
being really tired.)

Jul 23 '05 #5
Markus Dehmann <ma************@gmail.com> wrote in news:3a0ipdF66migkU1
@individual.net:
I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";

Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?


No. Quality of Implementation issue. Depends on your compiler.
Jul 23 '05 #6
ra******@hotmail.com wrote:
most commercial grade compilers will optimize it away. But the standard
doesn't guarantee it


On the other hand, a well-formed program can't tell whether that code
has been optimized away, because it has no observable behavior.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7
Pete Becker <pe********@acm.org> wrote in message news:<pP********************@giganews.com>...
ra******@hotmail.com wrote:
most commercial grade compilers will optimize it away. But the standard
doesn't guarantee it


On the other hand, a well-formed program can't tell whether that code
has been optimized away, because it has no observable behavior.

When will I be able to observe my program's behavior ?
What is a well-formed program ? may i ask you, a member of acm ????

Thank you
Jul 23 '05 #8
Just that wrote:

When will I be able to observe my program's behavior ?
What is a well-formed program ?


A well-formed program is one that conforms to the requirements of the
C++ Standard. Observable behavior is printed output, etc. If that code
is never executed, the output you see won't depend on whether the code
is left in the executable or removed.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #9
Just that wrote:
Pete Becker <pe********@acm.org> wrote in message
news:<pP********************@giganews.com>...
ra******@hotmail.com wrote:
> most commercial grade compilers will optimize it away. But the standard
> doesn't guarantee it
>


On the other hand, a well-formed program can't tell whether that code
has been optimized away, because it has no observable behavior.

When will I be able to observe my program's behavior ?
What is a well-formed program ? may i ask you, a member of acm ????


A certain slowdown might be measurable, though. Execution speed is a form of
"observable behavior," I'd say.

Markus
Jul 23 '05 #10
Markus Dehmann wrote:
Just that wrote:
Pete Becker <pe********@acm.org> wrote in message
news:<pP********************@giganews.com>...
ra******@hotmail.com wrote:
most commercial grade compilers will optimize it away. But the
standard doesn't guarantee it
On the other hand, a well-formed program can't tell whether that
code has been optimized away, because it has no observable behavior.

When will I be able to observe my program's behavior ?
What is a well-formed program ? may i ask you, a member of acm ????


A certain slowdown might be measurable, though. Execution speed is a
form of "observable behavior," I'd say.


I believe Pete was summarizing the standard's notion of observable behavior --
assuming that his "etc" includes something about volatile data. ;-)

Jonathan
Jul 23 '05 #11
Markus Dehmann wrote:

A certain slowdown might be measurable, though. Execution speed is a form of
"observable behavior," I'd say.


Not according to the language definition.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
4
by: Pelle Beckman | last post by:
Hi, I saw this code in an earlier post (not that there's anything wrong with it) #include <iostream> using std::cout; const int hour = 3600; const int min = 60;
3
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
5
by: David Krmpotic | last post by:
it's me again.. with quite interesting discovery :) I was tracing an error for 3 hours instead of 3 minutes because of this surprise. Recently I'm compiling this project in RELEASE with Debug...
1
by: Stabiplan BV | last post by:
Hi, I am trying to find const variables outside a class. Thur far I did not succeed. Without const, the variable is found but with const not. So, how can I get hold on global const variables...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
26
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am...
21
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
3
by: Chameleon | last post by:
The following code produces strange errors in mingw. Is a C++ problem or compiler problem? ---------------------------- #include <list> class A { static const int B = 0; std::list<intlst;...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.