使用非静态,非多态+空指针的未定义行为? [英] Undefined behaviour with Non-static, non-polymorphic + null pointer?

查看:68
本文介绍了使用非静态,非多态+空指针的未定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我很确定这是未定义的行为,尽管

它编译并''运行''(打印)在我的所有平台上都不存在:


#include< iostream>


class foo {

public:

void bar(){

std :: cout<< 你好邪恶的世界! << std :: endl;

if(this){

std :: cout<< 这存在 << std :: endl;

}

else {

std :: cout<< 这不存在! << std :: endl;

}

}

};


int main(){

foo * inst = 0;

inst-> bar();


返回0;

}


有人可以引用这一章的章节和经文来帮助我赢得

我当前的这是一个非常糟糕的主意我有什么争论?我会根据一些一般规则禁止

,并且没有例外

为它做了什么?或者它实际上是否合法并定义了

因为没有任何东西可以取消引用这个指针?


谢谢,

Alan

解决方案

Alan Woodland写道:


我很确定这是未定义的行为,尽管

它在我所有的b $ b平台上编译并运行(打印这不存在)这一事实:


#include< iostream>


class foo {

public:

void bar(){

std :: cout<< 你好邪恶的世界! << std :: endl;

if(this){

std :: cout<< 这存在 << std :: endl;

}

else {

std :: cout<< 这不存在! << std :: endl;

}

}

};


int main(){

foo * inst = 0;

inst-> bar();



这里你是使用具有无效值的指针(不是
指向任何对象)。那是不确定的行为。我不能快速地确定标准中确切的段落,但是我确定你可以在档案中找到它,只需搜索for

" dereference null pointer"。


>

返回0;

}


有人可以引用这一章的章节和经文来帮助我赢得

我当前的这是一个非常糟糕的主意我有什么争论?我会根据一些一般规则禁止

,并且没有例外

为它做了什么?或者它实际上是否合法并定义了

,因为没有任何东西可以取消引用这个指针?



表达式


inst-> bar()


是实际上


(* inst).bar()


已经取消引用空指针''inst''。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Victor Bazarov写道:


Alan Woodland写道:


>我很确定这是未定义的行为,尽管
它编译并且''运行'' (在我的所有平台上打印这并不存在):

#include< iostream>

类foo {
public:
void bar(){
std :: cout<< 你好邪恶的世界! << std :: endl;
if(this){
std :: cout<< 这存在 << std :: endl;
}
else {
std :: cout<< 这不存在! << std :: endl;
}
}
};

int main(){
foo * inst = 0;
inst- >巴();



这里你是使用具有无效值的指针(不是
指向任何对象)。那是不确定的行为。我不能快速地确定标准中确切的段落,但是我确定你可以在档案中找到它,只需搜索for

" dereference null pointer" ;.


>返回0;
}

有人可以引用这一章的章节和经文并帮助我赢得
我当前的这是一个非常糟糕的主意我有什么争论?我会根据某些一般规则预计它会被禁止,而且没有例外吗?或者它实际上是否合法和定义
因为什么都没有取消引用这个指针?



表达式


inst-> bar()


是实际上


(* inst).bar()


已经取消引用空指针''inst''。



谢谢。这很有趣,我以前从未真正了解过这种情况下

的影响。刚刚找到以下引用,应该说服某些人:


标准说p->

转换为(* p)。 (参见第5.2.5节),无论你如何切片,

* p是一个解除引用。取消引用空指针会导致未定义的

行为。


有些编译器可能会忽略转换,但这是
$ b的一部分$ b未定义部分行为。你不能依赖它在所有编译器上发生的事情 - 甚至不是你当前编译器的未来版本。


Alan


Alan Woodland写道:


谢谢。这很有趣,我以前从来没有真正意识到这种情况下的影响

。刚刚找到以下引用

应该说服某些人:



事件更有趣:根据5.2.5,这段代码:

struct X {static const int x = 0; };

int main(){

X * x = 0;

x-> n;

}


调用UB。


如果我不想懒得查找它,我可以告诉你是否


struct X {enum {x = 0}; };

int main(){

X * x = 0;

x-> n;

}


是否调用UB。


(我的意思是,他们真的可以做一个附录
的权威列表
UB'',因为在标准中找到这些只是分散的

真的很麻烦。


-

IYesNo yes = YesNoFactory.getFactoryInstance()。YES;

yes.getDescription()。equals(array [0] .toUpperCase());


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() {
std::cout << "hello evil world!" << std::endl;
if (this) {
std::cout << "this exists" << std::endl;
}
else {
std::cout << "this doesn''t exist!" << std::endl;
}
}
};

int main() {
foo *inst = 0;
inst->bar();

return 0;
}

Can someone please quote chapter and verse on this one and help me win
my current "this is a very bad idea" argument I''m having? I''d have
expected it to be forbidden under some general rule, and no exceptions
to have been made for it? Or is it actually really legal and defined
because nothing ever dereferences the this pointer?

Thanks,
Alan

解决方案

Alan Woodland wrote:

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() {
std::cout << "hello evil world!" << std::endl;
if (this) {
std::cout << "this exists" << std::endl;
}
else {
std::cout << "this doesn''t exist!" << std::endl;
}
}
};

int main() {
foo *inst = 0;
inst->bar();

Here you''re "using" the pointer that has an invalid value (does not
point to any object). That''s undefined behavour. I could not quickly
locate the exact passage in the Standard that says that it is, but I
am sure you can find a mention of it in the archives, just search for
"dereference null pointer".

>
return 0;
}

Can someone please quote chapter and verse on this one and help me win
my current "this is a very bad idea" argument I''m having? I''d have
expected it to be forbidden under some general rule, and no exceptions
to have been made for it? Or is it actually really legal and defined
because nothing ever dereferences the this pointer?

The expression

inst->bar()

is in fact

(*inst).bar()

which already dereferences the null pointer ''inst''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov wrote:

Alan Woodland wrote:

>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() {
std::cout << "hello evil world!" << std::endl;
if (this) {
std::cout << "this exists" << std::endl;
}
else {
std::cout << "this doesn''t exist!" << std::endl;
}
}
};

int main() {
foo *inst = 0;
inst->bar();


Here you''re "using" the pointer that has an invalid value (does not
point to any object). That''s undefined behavour. I could not quickly
locate the exact passage in the Standard that says that it is, but I
am sure you can find a mention of it in the archives, just search for
"dereference null pointer".

> return 0;
}

Can someone please quote chapter and verse on this one and help me win
my current "this is a very bad idea" argument I''m having? I''d have
expected it to be forbidden under some general rule, and no exceptions
to have been made for it? Or is it actually really legal and defined
because nothing ever dereferences the this pointer?


The expression

inst->bar()

is in fact

(*inst).bar()

which already dereferences the null pointer ''inst''.

Thanks. It''s funny, I''d never actually though about the implications of
that in this context before. Just found the following quote which ought
to convince certain people:

The Standard says that "p->" is
converted to "(*p)." (see section 5.2.5) and no matter how you slice it,
*p is a dereference. Dereferencing a null pointer results in undefined
behaviour.

Some compilers may ignore the conversion, but that''s part of the
"undefined" part of the behaviour. You cannot rely on it happening on
all compilers - not even future releases of your current compiler.

Alan


Alan Woodland wrote:

Thanks. It''s funny, I''d never actually though about the implications
of that in this context before. Just found the following quote which
ought to convince certain people:

Event funnier: According to 5.2.5, this code:
struct X { static const int x=0; };
int main() {
X*x=0;
x->n;
}

invokes UB.

And if I''d was not to lazy to look it up, I could tell you if

struct X { enum {x=0}; };
int main() {
X*x=0;
x->n;
}

invokes UB or not.

(I mean, they could really make an appendix "Authoritative List of
UB''s", because it''s really a nuisance to find these only scattered
around in the Standard)

--
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());


这篇关于使用非静态,非多态+空指针的未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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