代码有什么问题? [英] what is wrong in the code?

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

问题描述

以下是代码:

struct S {

int i;

int * p;

};

void main()

{

Struct S s;

int * p =& si;

p [0] = 4;

p [1] = 3;

sp = p;

sp [1] = 1;

sp [0] = 2;

}

我不知道为什么导致问题发生在最后一句话?

非常感谢!

解决方案

Le 20-04-2006,Sean Zhang < SE ********* @ gmail.com> écrit*:

以下是代码:
struct S {
int i;
int * p;
};
void main()
{
结构S s;
int * p =& si;
p [0] = 4;


OK:p [0] == * p == si

您的代码相当于

si = 4

p [1] = 3;


未定义的行为。 p [1]不存在!

s.p = p;


s.p =& s.i;

s.p [1] = 1;


UB

s.p [0] = 2;


OK}




Marc Boyer


实际上,错误只是在最后一句话中发生了!没有未定义

行为错误!

Sean Zhang

Marc Boyer ???é?????

Le 20-04-2006,Sean Zhang< se ********* @ gmail.com> a?crit?*:

以下是代码:
struct S {
int i;
int * p;
};
void main()
{
结构S s;
int * p =& si;
p [0] = 4;


好的:p [0] == * p == si
你的代码相当于
si = 4

p [1] = 3;



未定义的行为。 p [1]不存在!

sp = p;



sp =& si;

sp [1] = 1;



UB

sp [0] = 2;



OK

}



Marc Boyer




< blockquote> Sean Zhang说:

以下是代码:
struct S {
int i;
int * p;
};
void main()


这是你的第一个问题。名为''main'的函数由

系统调用,因此接口规范由

语言精心定义。这不是你的决定。有两种核心形式可以在所有托管实现中移植,而你需要的是:


int main(void)


{
Struct S s;


这没关系。它为结构S创建存储。

int * p =& s.i;


这很好。它需要并存储s.i的地址。

p [0] = 4;


这也很好。它将值4存储在s.i中。

p [1] = 3;




这不好。您正在尝试将值3写入不存在的数组元素

。结果是未定义的行为。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
p[1] = 3;
s.p = p;
s.p[1] = 1;
s.p[0] = 2;
}
I don''t why cause the problem occurred in the last sentence?
Thanks a lot!

解决方案

Le 20-04-2006, Sean Zhang <se*********@gmail.com> a écrit*:

The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
OK: p[0] == *p == s.i
You code is equivalent to
s.i= 4
p[1] = 3;
Undefined behavior. p[1] does not exists !
s.p = p;
s.p = &s.i;
s.p[1] = 1;
UB
s.p[0] = 2;
OK }



Marc Boyer


Acctually, The error is just happedn in the last sentence! No undefined
behavior error!

Sean Zhang
Marc Boyer ???é?????

Le 20-04-2006, Sean Zhang <se*********@gmail.com> a ??crit?*:

The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;



OK: p[0] == *p == s.i
You code is equivalent to
s.i= 4

p[1] = 3;



Undefined behavior. p[1] does not exists !

s.p = p;



s.p = &s.i;

s.p[1] = 1;



UB

s.p[0] = 2;



OK

}



Marc Boyer




Sean Zhang said:

The following is the code:
struct S {
int i;
int * p;
};
void main()
Here is your first problem. The function called ''main'' is called by the
system, and so the interface specification is carefully defined by the
language. It isn''t your decision. There are two core forms that are
portable across all hosted implementations, and the one you need is:

int main(void)

{
Struct S s;
This is okay. It creates storage for a struct S.
int * p = &s.i;
This is fine. It takes and stores the address of s.i.
p[0] = 4;
This is fine too. It stores the value 4 in s.i.
p[1] = 3;



This is not fine. You are trying to write the value 3 into an array element
that does not exist. The result is undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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