473,484 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Null pointer to cout

Hi,

this might be intentional, but I don't see a reason, why.

Running the program
----------------------
#include <iostream>
using namespace std;

int main(){
char *x=NULL;
cout<<"before"<<endl;
cout<<x<<endl;
cout<<"after"<<endl;
}
-------------------------

I would expect to see

-------------
before

after
-------------

but instead I get only

------------
before
------------

The program exits normally. Is that behaviour correct or is it a bug?

Linux i386, gcc version 4.0.2 20050901 (prerelease) (SUSE Linux)

Thanks,

Ralf
--
There is only one _ in my address
May 24 '06 #1
14 9292

Ralf Goertz wrote:
Hi,

this might be intentional, but I don't see a reason, why.

Running the program
----------------------
#include <iostream>
using namespace std;

int main(){
char *x=NULL;
This is not the empty string. For that you should have char *x=""; cout<<"before"<<endl;
cout<<x<<endl; Here you try to cout something that does not exist. cout<<"after"<<endl;
}
-------------------------

I would expect to see

-------------
before

after
-------------

but instead I get only

------------
before
------------

The program exits normally. Is that behaviour correct or is it a bug?
That is correct behaviour. Null-pointers never denote the empty string.

/Peter
Linux i386, gcc version 4.0.2 20050901 (prerelease) (SUSE Linux)

Thanks,

Ralf
--
There is only one _ in my address


May 24 '06 #2
>>"Here you try to cout something that does not exist. "

But I do not understand why the third cout printing after does not work
?

I have observed a similar behaviour with CIN
int a;
cin>>a;

int b;
cin>>b;

cout<<b;
Now if the first input to the program is given as a string the second
cin, does not come into picture at all.
Some garbage gets printed as b remains uninitialised.
regards,
Amir Kamerkar

May 24 '06 #3
Ralf Goertz posted:
Hi,

this might be intentional, but I don't see a reason, why.

Running the program
----------------------
#include <iostream>
using namespace std;

int main(){
char *x=NULL;

The pointer "x" now contains the following memory address:

0x00000000

(Note to nit-pickers: Yes I realise that it needn't be all-bits-zero.)

cout<<"before"<<endl;
Here you print "before".
cout<<x<<endl;

Here you're forcing the computer to read from the memory location:
0x00000000
The C++ Standard says that this is Undefined Behaviour.

cout<<"after"<<endl;

You've already gone messing around with invalid memory, so your program's
already messed up.
-Tomás
May 24 '06 #4
Tomás wrote:
Here you're forcing the computer to read from the memory location:
0x00000000
The C++ Standard says that this is Undefined Behaviour.

cout<<"after"<<endl;

You've already gone messing around with invalid memory, so your program's
already messed up.


Okay, that I can understand. It took me quite a while to find this problem
at
all because I actually used a QString variable str and didn't know that in
my
particular case str.isNull() could ever be true. I would have found the
problem faster if the problem had segfaulted. Why didn't it if it actually
messed around with invalid memory?

Ralf

--
There is only one _ in my address
May 24 '06 #5
Ralf Goertz posted:
I would have found
the problem faster if the problem had segfaulted. Why didn't it if it
actually messed around with invalid memory?

Write an e-mail to your operating system's manufacturer and they might tell
you -- because Standard C++ certainly says that all bets are off once you
start messing with null pointers.

However, on your particular platform, it may be perfectly okay to mess with
a null pointer, and it may have absolutely no effect:

int *p = 0;

*p = 5; /* No effect */
Then again, your operating system just might not be complaining about
memory being messed around with.
-Tomás
May 24 '06 #6
Ralf Goertz wrote:
Tomás wrote:
Here you're forcing the computer to read from the memory location:
0x00000000
The C++ Standard says that this is Undefined Behaviour.


I would have found the problem faster if the problem had segfaulted. Why
didn't it if it actually messed around with invalid memory?


That's the thing about undefined behavior, it is undefined. It could
mean segfaulting, or it could mean doing any other arbitrary thing.

It is my guess that your implementation sets the failbit for cout when
you try to insert a NULL pointer. You can force an exception to be
thrown when that happens by calling:
cout.exceptions(ios_base::failbit) ;

Not exactly a segfault, but it will still get your attention.

--
Alan Johnson
May 24 '06 #7

Tomás wrote:
char *x=NULL;
cout<<x<<endl;
Here you're forcing the computer to read from the memory location:
0x00000000


Is there no built-in equivalent to the %p printf() format specifier?

May 24 '06 #8

C. Benson Manica wrote:
Tomás wrote:
char *x=NULL;
cout<<x<<endl;

Here you're forcing the computer to read from the memory location:
0x00000000


Is there no built-in equivalent to the %p printf() format specifier?


Yes:

void f(char* s)
{
std::cout << static_cast<void*>(s);
}

operator<< for std::ostream is overloaded for the char* family; these
are being interpreted as c-style strings.
Jonathan

May 24 '06 #9
Jonathan Mcdougall <jo***************@gmail.com> wrote:
void f(char* s)
{
std::cout << static_cast<void*>(s);
} operator<< for std::ostream is overloaded for the char* family; these
are being interpreted as c-style strings.


If s is NULL, is that defined?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 25 '06 #10
Christopher Benson-Manica wrote:
Jonathan Mcdougall <jo***************@gmail.com> wrote:
void f(char* s)
{
std::cout << static_cast<void*>(s);
}

operator<< for std::ostream is overloaded for the char* family; these
are being interpreted as c-style strings.


If s is NULL, is that defined?


No, it is undefined behavior.
Jonathan

May 25 '06 #11

Christopher Benson-Manica skrev:
Jonathan Mcdougall <jo***************@gmail.com> wrote:
void f(char* s)
{
std::cout << static_cast<void*>(s);
}

operator<< for std::ostream is overloaded for the char* family; these
are being interpreted as c-style strings.


If s is NULL, is that defined?


That depends on what you mean with your question. For the function
shown, the output is well-defined also if s is null. For the
overloaded operator in in char* family, the output is NOT defined for a
null-pointer - exactly as was demonstrated by the first post.

/Peter

May 25 '06 #12

peter koch wrote:
That depends on what you mean with your question. For the function
shown, the output is well-defined also if s is null. For the
overloaded operator in in char* family, the output is NOT defined for a
null-pointer - exactly as was demonstrated by the first post.


Well, since AFAIK

printf( "Pointer=%p\n", static_cast<void*>(some_pointer) );

is valid even if some_pointer is NULL, it seems that there is indeed no
builtin way to print a null pointer to std::cout...

May 25 '06 #13
C. Benson Manica wrote:
peter koch wrote:
That depends on what you mean with your question. For the function
shown, the output is well-defined also if s is null. For the
overloaded operator in in char* family, the output is NOT defined for a
null-pointer - exactly as was demonstrated by the first post.


Well, since AFAIK

printf( "Pointer=%p\n", static_cast<void*>(some_pointer) );

is valid even if some_pointer is NULL, it seems that there is indeed no
builtin way to print a null pointer to std::cout...


Either your don't understand the answers or we don't understand the
question.

void f(char* s)
{
// ok if s points to a valid c-style string
// undefined behavior if s==0
std::cout << s;

// always valid, outputs the pointer value
std::cout << static_cast<void*>(s);
}
Jonathan

May 25 '06 #14

Jonathan Mcdougall wrote:
Either your don't understand the answers or we don't understand the
question.
Well, you're the first to understand it...
// always valid, outputs the pointer value
std::cout << static_cast<void*>(s);


....since that's what I was aiming at. Thanks.

May 25 '06 #15

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

Similar topics

4
4513
by: _ivan | last post by:
Hello, So I think the answer to this question is going to be rather simple. Basically I have a pointer to a class, and I want to have a function to call to initialize it. After this...
69
7364
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
6
3086
by: nish.parikh | last post by:
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...
13
2735
by: Jacek Dziedzic | last post by:
Hello! I have a piece of code that needs to display a formatted table of pointers using streams, with the pointers represented as hex values. It looks more or less like this: #include...
35
2746
by: fcvcnet | last post by:
Hi, This is c code , but the question is same to c++ £¨new/delete£©. char* p = malloc(10); strcpy(p, "hello"); printf("%s\n", p); free(p); // free just add p to the free list of memory, and...
23
7363
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
7
4858
by: Robin Imrie | last post by:
Hi, I have some code like this.... MyObject ^o = gcnew MyObject(); My2ndObject ^obj2 = o->GetObject(); if( obj2 != NULL ) {
11
1522
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public:...
1
4256
by: zackp | last post by:
Hello, I am learning C++ lately. This morning, out of curiosity I constructed a simple code below, but to my surprise, when char* is used, the program fails to print almost anything. The only...
0
6943
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
7100
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
7135
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
7152
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
5399
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,...
1
4833
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...
0
3041
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...
0
1346
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 ...
1
583
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.