5

I have the following code snippet:

#ifdef DO_LOG
#define log(p) record(p)
#else
#define log(p)
#endif

void record(char *data){
.....
.....
}

Now if I call log("hello world") in my code and DO_LOG isn't defined, will the line be compiled, in other words will it eat up the memory for the string "hello world"?

P.S. There are a lot of record calls in the program and it is memory sensitive, so is there any other way to conditionally compile so that it only depends on the #define DO_LOG?

4
  • 2
    You shouldn't implement your own logging library. There's enough of them out there. Here's what I'm using: templog.org. Commented Mar 24, 2010 at 9:56
  • 2
    @sbi I don't know. I've written my own one and it works beautifully. It is the best way to learn how to do something. :-) Commented Mar 24, 2010 at 10:07
  • 1
    @Konrad: I did so, too, but it's hard to be better than the accumulated wisdom of an established library. Commented Mar 24, 2010 at 10:18
  • @sbi: The problem with logging libraries is that everyone does them differently, and everyone prioritizes different features. there's a reason Boost still doesn't have one. No two people can agree on what a good logging library is. I'd say this is an area where writing your own is often (not always) a good idea. Commented Mar 24, 2010 at 13:20

4 Answers 4

14

This should be trivial to verify for yourself by inspecting the resulting binary.

I would say "no", since the expression totally goes away, the compiler will never see the string (it's removed by the preprocessor's macro expansion).

Sign up to request clarification or add additional context in comments.

4 Comments

This is correct. And writing a program to verify it took me all of 30 seconds.
@unwind & @Neil: I'd be interested how you'd verified that the observed behavior isn't just a peculiarity of your compiler.
@sbi: I didn't, I'll gladly admit. I do think that the logic I use in my answer is sound; the preprocessor macro must work like defined, i.e. the string literal disappears from the source. Feel free to quote standards if that's wrong, I'd be happy to know.
I'm not disputing what you say, I'm just disputing that one can come to the same conclusion by testing with any one compiler/std lib combination.
4

Since the preprocessor runs before the compiler, the line will not even exist when the compiler runs. So the answer is no, it does not use any memory at all.

Comments

4

No, it will not be in the binary. It will not even be compiled - the preprocessor will expand it into an empty string prior to the compilation, so the compiler will not even see it.

Comments

2

No. The preprocessor is executed prior to compilation, and so the code will never even be seen. I would like to add, though, that if you are interested in adding logging to your C++ application, you might want to use the Log4Cxx library. It uses similar macros which you can completely elide from your application, but when logging is enabled, it supports several different levels of logging (based on importance/severity) as well as multiple different "appenders" to which to send logging output (e.g. syslog, console, files, network I/O, etc.).

The full API documentation may be found at Log4Cxx API docs. Also, if you have any Java developers on board who have used Log4J, they should feel right at home with Log4Cxx (and convince you to use it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.