非指定的指针 [英] Unintialized Pointer

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

问题描述

亲爱的专家,


在以下计划中


A级

{

public:

void print()

{

cout<<"" Hello World"<< endl; < br $>
}

};


int main()

{

A * a;

(* a).print();

}


如果我们声明一个类指针是未初始化的,我们试图

调用一个成员函数(它不访问

类的任何数据成员)它工作正常。为什么?

标准是否允许这样做?


问候,

Siddharth

Dear Experts,

In the following Program

class A
{
public:
void print()
{
cout<<"Hello World"<<endl;
}
};

int main()
{
A *a;
(*a).print();
}

If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?

Regards,
Siddharth

推荐答案

siddhu写道:
siddhu wrote:

亲爱的专家,


以下节目


A级

{

公开:

无效打印()

{

cout<<<"" Hello World"<< endl;

}

};


int main()

{

A * a;

(* a).print() ;

}


如果我们声明一个未初始化的类指针,我们尝试

调用一个成员函数(这是没有访问

类的任何数据成员)它工作正常。为什么?

标准是否允许这样做?
Dear Experts,

In the following Program

class A
{
public:
void print()
{
cout<<"Hello World"<<endl;
}
};

int main()
{
A *a;
(*a).print();
}

If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?



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

可以做任何事情,包括但不限于:


*正常工作按预期

*核心倾销

*重新格式化您的硬盘

*致电NORAD并开始第三次世界大战

*谋杀船员并锁定吊舱舱门。 />

请注意,正如预期的那样工作是其中一个选项,因为它是任何东西的一部分



The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.

Note that working "as expected" is one of the options, since it is part
of "anything".


red floyd写道:
red floyd wrote:

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

可以做任何事情,包括但不限于:


*正常工作按预期

*核心倾销

*重新格式化您的硬盘

*致电NORAD并开始第三次世界大战

*谋杀船员并锁定吊舱门。
The standard says that such behavior is undefined. Undefined behavior
can do anything, including but not limited to:

* working "as expected"
* core dumping
* reformatting your hard drive
* calling NORAD and starting World War III
* Murdering the crew and locking the pod bay doors.



我从未理解为什么这是一个如此常见的笑话。要说

" undefined behavior"意味着<插入物理上不可能和完全

古怪的东西在这里>。


当然,表达式的字母是未定义的行为。可能允许

编译器要做*任何事情*。但是,我认为那个

表达式的精神只是意味着效果可以是随机的,因为价值

无法得到保证。在尝试读取

未初始化变量的特定上下文中,这意味着该变量可以具有任何值,

并且标准没有指定特定值。它当然确实没有意味着试图读取未初始化的变量将调用

NORAD并启动WW3。


为什么人们不能在实践中告诉可能发生的事情*而不是

想出所有过度重复的过度笑话?

I have never understood why it''s such a common "joke" to say that
"undefined behavior" means <insert physically impossible and completely
wacky thing here>.

Sure, the letter of the expression "undefined behaviour" might "allow"
the compiler to do *anything*. However, I think that the spirit of that
expression simply means "the effects can be random because the values
cannot be guaranteed". In the specific context of trying to read an
uninitialized variable it means that the variable could have any value,
and no specific value is specified by the standard. It certainly does
not mean that trying to read an uninitialized variable will "call
NORAD and start WW3".

Why can''t people just tell what may happen *in practice* instead of
coming up with all the tired overly-repeated jokes?


siddhu写道:

siddhu wrote:


如果我们声明一个未初始化的类指针,我们尝试

调用一个成员函数(它不访问

类的任何数据成员)它工作正常。为什么?

标准是否允许这样做?
If we declare a class pointer which is uninitialized and we try to
call a member function (which is not accessing any data member of the
class) it works fine. Why?
Does standard allow this?



未定义的行为没有定义的行为。


Brian

There is no defined behavior for Undefined Behavior.


Brian


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

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