自我分配 - 未定义? [英] Self-assignment -- undefined?

查看:63
本文介绍了自我分配 - 未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码段是否表现出未定义的行为(请原谅这个代码的愚蠢

):


int i = 5;


i = i;


或者更复杂的形式呢:


void Func(int * const p,int const * const q)

{

* p = * q;

}


int main(无效)

{

int i = 5;


Func( & i,& i);


返回0;

}


我很怀疑这可能是一个序列点被违反。


-


Frederick Gotham


Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I''m suspicious that maybe a sequence point is being violated.

--

Frederick Gotham

推荐答案




Frederick Gotham在11/09/06 11:24写道:


Frederick Gotham wrote On 11/09/06 11:24,:

下面的代码片段是否表现出未定义的行为(请原谅这个代码的愚蠢

):


int i = 5;


i = i;
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;



No.

No.


或者更复杂的形式呢:


void Func(int * const p,int const * const q)

{

* p = * q;

}


int main(无效)

{

int i = 5;


Func(& i,& i);


返回0;

}
Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}



No.

No.


我怀疑可能是一个序列点被违反了。
I''m suspicious that maybe a sequence point is being violated.



没有违反序列点这样的事情。

你可能指的是规则这需要某些

操作 - 对同一个对象进行两次修改,对于

示例 - 由至少一个序列点分隔,

但上面的代码片段并没有违反这些规则。


-
Er ********* @ sun.com

There''s no such thing as "violating a sequence point."
You''re probably referring to the rules that require certain
operations -- two modifications to the same object, for
example -- to be separated by at least one sequence point,
but the code snippets above do not break those rules.

--
Er*********@sun.com


嗨弗雷德里克,
Hi Frederick,

下面的代码片段是否显示未定义的行为(请原谅这个代码的愚蠢

):


int i = 5;


i = i;


或者更复杂的形式呢:


无效Func(int * const p,int const * const q)

{

* p = * q;

}


int main(无效)

{

int i = 5;


Func(& i,& i);


返回0;

}


我怀疑可能是一个序列点被侵犯了。
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I''m suspicious that maybe a sequence point is being violated.



对于那些琐碎的例子,A​​FAIK没有。当然,如果你的Func假定

参数的不同地址能够正常工作,事情就会变得棘手。


干杯,

Loic。

For those trivial examples, AFAIK no. Of course, things can become
tricky for instance if your Func assumes different addresses for the
arguments to work correctly.

Cheers,
Loic.


lo ****** @ gmx.net 写道:

>

嗨Frederick,

下面的代码片段是否显示未定义的行为(请原谅这个代码的愚蠢

):


int i = 5;


i = i;


或者更复杂的形式呢:


void Func(int * const p,int const * const q)

{

* p = * q;

}


int main(无效)

{

int i = 5;


Func(& i,& i);


返回0;

}


我''米怀疑可能是一个序列点被侵犯。
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I''m suspicious that maybe a sequence point is being violated.



对于那些微不足道的例子,A​​FAIK没有。当然,如果你的Func假定

参数的不同地址可以正常工作,那么事情就会变得棘手。


For those trivial examples, AFAIK no. Of course, things can become
tricky for instance if your Func assumes different addresses for the
arguments to work correctly.



例如,如果使用Func():


* p =(* q)++;


-

+ ------------------------- + ---- ---------------- + ----------------------- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |

+ ------------------------- + --------- ----------- + ----------------------- +

不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>

For example, if Func() used:

*p = (*q)++;

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


这篇关于自我分配 - 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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