473,411 Members | 2,019 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,411 software developers and data experts.

"Null" I/O Stream?

Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...
}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?

Or is there a better way?

Thanks!
-bw

Dec 12 '06 #1
4 2183
On Dec 13, 1:04 am, isanb...@gmail.com wrote:
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...

}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?
What I can tell ostream does not have any constructor taking 0 as an
argument, what were you hoping to accomplish?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?
Perhaps you want something like this (I'm not good at macros and it's
untested, but it should give you an idea):

#ifndef NDEBUG
#define DEBUG(x) (std::cerr << x;)
#else
#define DEBUG(x) // not sure about this line, might need () or
something
#endif

And then use it like:
DEBUG("Debug-message")
or
DEBUG(foo())

--
Erik Wikström

Dec 13 '06 #2
Hi

er****@student.chalmers.se wrote:
What I can tell ostream does not have any constructor taking 0 as an
argument, what were you hoping to accomplish?
How about
"public: explicit basic_ostream(basic_streambuf<char_type, traits>* sb)"?

Anyway, while you can construct this stream, you cannot use it. Outputting
to a stream invokes something equivalent to rdbuf()->sputc(...). Undefined
behavior by dereferencing a null pointer.
>On Dec 13, 1:04 am, isanb...@gmail.com wrote:
>As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?
I don't think that this is specified. I might be wrong.
Perhaps you want something like this (I'm not good at macros and it's
untested, but it should give you an idea):

#ifndef NDEBUG
#define DEBUG(x) (std::cerr << x;)
#else
#define DEBUG(x) // not sure about this line, might need () or
something
#endif
Then DEBUG(1 << 4) outputs "1" and then "4"?
People might expect 16.
Another suggestion:

#ifndef NDEBUG
std::ostream& dout = std::cerr;
#else
struct dummy_ostream {
template<typename Tdummy_ostream& operator<< (T) { return *this; }
} dout;
#endif

If you need other members, add them...

Markus

Dec 13 '06 #3
is******@gmail.com a écrit :
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...
}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?

Or is there a better way?

Thanks!
-bw
Make the code unreachable:
#define DOUT while(0) std::cout

And turn off warning concerning unreachable code

Beware of bug like:
DOUT<<"Result: "<<myImportantFunction()<<std::endl;
because myImportantFunction() won't be executed either (same as assert).

Michael
Dec 13 '06 #4
is******@gmail.com wrote:
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream cnull(0);

You need a null streambuf. It's easy to write.
Just define the overflow() function to do nothing (throws
the characters away) and use that to initialize your output
stream.
Dec 13 '06 #5

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

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
1
by: putty | last post by:
I found a few posts of people asking about insertCell()/insertRow() not working in IE6 SP2, and a few others about getting "null is null or not an object" errors, but no one posted a solution...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
2
by: Rudolf Ball | last post by:
Hi NG, when trying to drag a UserControl from the toolbox on a Form, this exception rises: The exception was ""null" is not a valid value for the 'stream'.". ? Thank you
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
1
by: mulham.haffar | last post by:
Hi, im writing an application that uses tcpclient to connect. I have a class which makes the whole network stuff.. As below: Public Class Connections Private Client as tcpclient Private...
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
2
by: crabsdf | last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream. Here is full code package...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.