473,699 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3498
"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 PRINTDEBUGMESSA GE(a) cerr << (a)
#else
#define PRINTDEBUGMESSA GE(a)
#endif

Now, when you use

PRINTDEBUGMESSA GE("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(str ing) cerr << (string)
#elseif
#else
#define DEBUG_PRINT(str ing)
#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("de bug 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:3a0ipdF66m igkU1
@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******@hotmai l.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******* *************@g iganews.com>...
ra******@hotmai l.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******* *************@g iganews.com>...
ra******@hotmai l.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

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

Similar topics

39
3081
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
1636
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
13563
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
6596
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 info turned on so that it creates .PDB files and I can see the line numbers in stack trace. To my surprise, release version of .pdb pointed me only to the beginning of
1
1727
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 using reflection? Regards, Rob
5
11321
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 always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
26
4415
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 interested in when making a member private is to disallow the modification of the value of that member (read-only member), then how about doing the following:
21
2745
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
3
3101
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; void calc();
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.