std::cout and NULL character pointer

N

nish.parikh

Hi,

I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?

example:
#include <iostream>
int main( int argc, char *argv[] ) {
char *q = 0;
std::cout << q << "\n";
std::cout << "Program ended\n";
}

Program ended does not get printed when i run this program.

I am working on solaris platform with gcc 3.2.2.

Let me know if this is not the correct group for such questions.
Any help would be appreciated.

Thanks,
Nish.
 
R

Ron Natalie

Hi,

I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?

example:
#include <iostream>
int main( int argc, char *argv[] ) {
char *q = 0;
std::cout << q << "\n";
std::cout << "Program ended\n";
}

Program ended does not get printed when i run this program.

It's undefined behavior to pass a null pointer here.
 
D

DeltaOne

Hi Nish,
As Ron told this is a undefined behaviour. The reason is as follows.
When you give a nummvalue to a pointer that is by assigning it 0 or
NULL then it means to the compiler that this pointer do not point to
the location where data (valid data) is there. In compilers they may
map this thing to a range of memory area. This memory area range is in
order used by the CPU to throw a software interrupt when ever some
thing is reffered to any of these areas. This range is used for this
purpose only.

This is as far as i remember the initial some KB of memory. But its
processor dependent. So never try this or you will get run time
undefined behaviour.If you find something more you can share with me.

~Regards
DeltaOne
 
J

Jaspreet

Hi,

I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?

example:
#include <iostream>
int main( int argc, char *argv[] ) {
char *q = 0;
std::cout << q << "\n";
Why do you need to do this ? Any specific reason ? Seems to me to be a
case of undefined behaviour which should be avoided.
std::cout << "Program ended\n";
}

Program ended does not get printed when i run this program.

I am working on solaris platform with gcc 3.2.2.
Same behaviour on gcc3.2.2 on linux also.
 
R

Rapscallion

I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?

example:
#include <iostream>
int main( int argc, char *argv[] ) {
char *q = 0;
std::cout << q << "\n";
std::cout << "Program ended\n";
}

You need to define a special operator<<. This should work:

std::eek:stream& operator<< (std::eek:stream& os, const char* p) {
if (p) {
os.write (p, strlen (p) +1);
} else {
os.write ("NULL", sizeof("NULL"));
}
return os;
}
 
N

nish.parikh

Thanks everyone for responding. I think the behavior should not be
undefined. As per ansidraft I found at
http://members.fortunecity.com/dumb...b-iostreams.html#lib.ostream.formatted.reqmts
and also comments in the STL code, seems like the bad bit for cout
stream would be set, when we try to do a cout << (char*)0; as a result
of which no subsequent calls to cout will print anything until the bad
state is cleared.

Nish.

I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?

example:
#include <iostream>
int main( int argc, char *argv[] ) {
char *q = 0;
std::cout << q << "\n";
std::cout << "Program ended\n";
}

You need to define a special operator<<. This should work:

std::eek:stream& operator<< (std::eek:stream& os, const char* p) {
if (p) {
os.write (p, strlen (p) +1);
} else {
os.write ("NULL", sizeof("NULL"));
}
return os;
}
 
R

Ron Natalie

Thanks everyone for responding. I think the behavior should not be
undefined. As per ansidraft I found at
http://members.fortunecity.com/dumb...b-iostreams.html#lib.ostream.formatted.reqmts
and also comments in the STL code, seems like the bad bit for cout
stream would be set, when we try to do a cout << (char*)0; as a result
of which no subsequent calls to cout will print anything until the bad
state is cleared.
I can't comment on that draft (I refuse to reference a site with that
name), but I can tell you that passing a NULL to Standard constructs
expecting a null-terminated string is almost universally undefined
behavior. There is no guarantee of squat. It might set the bad
bit, it might just output nothing, it might cause monkeys to erupt
from the user's posterior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top