473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::cout and NULL character pointer

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.

Jul 23 '05 #1
6 3121
ni*********@gma il.com wrote:
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.

Jul 23 '05 #2
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

Jul 23 '05 #3
ni*********@gma il.com wrote:
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.
Let me know if this is not the correct group for such questions.
Any help would be appreciated.

Thanks,
Nish.


Jul 23 '05 #4
ni*********@gma il.com wrote:
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::ostream& operator<< (std::ostream& os, const char* p) {
if (p) {
os.write (p, strlen (p) +1);
} else {
os.write ("NULL", sizeof("NULL")) ;
}
return os;
}

Jul 23 '05 #5
Thanks everyone for responding. I think the behavior should not be
undefined. As per ansidraft I found at
http://members.fortunecity.com/dumbf...rmatted.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.
Rapscallion wrote:
ni*********@gma il.com wrote:
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::ostream& operator<< (std::ostream& os, const char* p) {
if (p) {
os.write (p, strlen (p) +1);
} else {
os.write ("NULL", sizeof("NULL")) ;
}
return os;
}


Jul 23 '05 #6
ni*********@gma il.com wrote:
Thanks everyone for responding. I think the behavior should not be
undefined. As per ansidraft I found at
http://members.fortunecity.com/dumbf...rmatted.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.
Jul 23 '05 #7

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

Similar topics

1
3410
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the locale correctly, say to it_IT.utf8 (on UNIX). However, when reading in text from a file using:
2
3302
by: Pmb | last post by:
I've noticed a lot of people preferring std::cout over cout. May I ask why you prefer one over the other? thanks Pmb
6
2364
by: clilley | last post by:
The following code causes a segmentation fault on DEC Tru64: foo.cc (built into libFoo.so) //--------------------------- include <iostream> bool createFoo() { std::cout << "createFoo" << std::endl; }
8
3469
by: jean.daniel.michaud | last post by:
Hi all, Something I don't get. The code is: // snippet on #include <list> #include <iostream> int main()
19
33430
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
6
60960
by: Roger | last post by:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the language using various sources. This sounds stupid, but I am really confused on this... I was wondering why we have to write a C++ program with this: #include <iostream> using std::cout; using std::endl;
5
2603
by: wongjoekmeu | last post by:
Dear All, I have written a small program to read in from console a user string. I wanted to be able to read in a string containing of all sorts of characters untill the user press enter. I have to use in my program the scanf function. The program works fine, but there is one exception. When I don't insert any input, but simply press enter, I want actually to pop up asking the user to enter a valid input. But it seems according to my...
11
6749
by: Adrian | last post by:
Is it possible to save a copy of cout the same way you can save a copy of stdout in C (I know C version is portable, but unix portable is good enough for me). I have to use a library which closes stdout on init because its used to mostly in background processes but I need it for an interactive process. Library cant be changed unfortunately. I tried the following but the C++ version doesn't work, I guess because it only shares the...
58
4842
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better" style and what the pros and cons are (I'm using cout as example, but it really applies to any similar construct): 1) using std::cout;
0
9531
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
9345
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
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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...
0
6609
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();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
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.