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 14 9337
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
>>"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
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
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
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
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::fail bit) ;
Not exactly a segfault, but it will still get your attention.
--
Alan Johnson
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?
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<voi d*>(s);
}
operator<< for std::ostream is overloaded for the char* family; these
are being interpreted as c-style strings.
Jonathan
Jonathan Mcdougall <jo************ ***@gmail.com> wrote: void f(char* s) { std::cout << static_cast<voi d*>(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)cybers pace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 function is called, the pointer is still NULL.
Here is a simplified version, but basically the same:
|
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
|
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 argc, char *argv ) {
char *q = 0;
std::cout << q << "\n";
|
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 <iostream>
#include <iomanip>
using namespace std;
|
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 do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
| |
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 in an
array. The application compiles but aborts without giving me any useful
information.
What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array of...
|
by: Robin Imrie |
last post by:
Hi,
I have some code like this....
MyObject ^o = gcnew MyObject();
My2ndObject ^obj2 = o->GetObject();
if( obj2 != NULL )
{
|
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:
void bar() {
|
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 thing I got is the following:
% : np =
As soon as I use a pointer to another type, everything behaves as
|
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...
|
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,...
| |
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |