空指针cout [英] Null pointer to cout

查看:59
本文介绍了空指针cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这可能是有意的,但我没有看到原因,为什么。


运行程序

----------------------

#include< iostream>

使用命名空间std;


int main(){

char * x = NULL;

cout<<"在<<< endl;

cout<< x<< endl;

cout<<""<<< endl;

}

-------------------------


我希望看到


-------------




之后

-------------


但我只得到


------------

之前

------------


程序正常退出。这种行为是正确的还是一个错误?


Linux i386,gcc版本4.0.2 20050901(预发布)(SUSE Linux)


谢谢,


拉尔夫

-

我的地址中只有一个_

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

推荐答案



Ralf Goertz写道:

Ralf Goertz wrote:

这可能是故意的,但我不知道为什么会看到原因。
运行程序
----------------------
#include< iostream>
使用命名空间std;

int main(){
char * x = NULL;


这不是空字符串。为此你应该有char * x ="" ;; cout<<<" before"<< endl;
cout<< x<< endl;
在这里你尝试cout不存在的东西。 cout<<<" after"<< endl;
}
-------------------------

我希望在

之后看到

-------------

-------------

但我只得到了

------------
之前
------------

程序正常退出。这种行为是正确的还是一个错误?


这是正确的行为。空指针永远不会表示空字符串。


/ Peter
Linux i386,gcc版本4.0.2 20050901(预发布)(SUSE Linux)
谢谢,

拉尔夫
-
我的地址中只有一个_
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





>>"在这里你尝试cout不存在的东西。


但是我不明白为什么第三次cout打印后不起作用




我已观察到与CIN类似的行为

int a;

cin>> a;


int b;

cin>> b;


cout<< b;

现在,如果程序的第一个输入是以字符串形式给出的第二个

cin,根本没有进入画面。

有些垃圾会被打印,因为b仍然没有被初始化。

问候,
Amir Kamerkar

>>"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发布:
Ralf Goertz posted:


运行程序
---------------- ------
#include< iostream>
使用命名空间std;

int main(){
char * x = NULL;



指针x现在包含以下内存地址:


0x00000000


(对于挑选者的注意事项:是的我意识到它不一定都是-bits-zero。)


cout<<" before"<< endl;


在此处打印之前。

cout<< x<< endl;



这里你强迫计算机从内存位置读取:

0x00000000

C ++标准说这是未定义的行为。


cout<<" after"<<< endl;
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;



你有因为无效的记忆已经消失了,所以你的程序已经搞砸了。

-Tomás


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


这篇关于空指针cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆