这个定义明确吗? [英] Is this well-defined?

查看:74
本文介绍了这个定义明确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我一直在使用这样的代码:


* p ++ =''x'';

* p ++ = * q ++;


现在很长一段时间了。有用。下面的代码编译没有

警告并正确运行。夹板也不会抱怨

这些线。


这是根据标准定义的吗?关于序列点,我读了一点

,而没有像i = i ++那样做。这个

似乎是相似的。如果您正在使用和

修改* same *变量,不止一次,没有

干预序列点,这只是一个问题吗?


特别是,


* p ++ = * q ++;


保证取消引用p和q,并执行作业,

之前是否增加了?


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

#include< stdio.h>


int main(无效)

{

char s [] =" some\xt\\\
in\\\
several\\\
lines\ n" ;;

char t [100],* src,* dest;


//用CRLF替换每个LF

for(src = s,dest = t; * src;){

if(* src ==''\ n'')

* dest ++ ='' \'';;

* dest ++ = * src ++;

}

* dest =''\ 0'';


fputs(t,stdout);

返回0;

}

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


谢谢,

-

Tom Zych

这个电子邮件地址将在某个时候到期以阻止垃圾邮件发送者。

永久地址:echo''g******@cbobk.pbz''| rot13

Hi. I''ve been using code like this:

*p++ = ''x'';
*p++ = *q++;

for a long time now. It works. The code below compiles with no
warnings and runs correctly. Even splint doesn''t complain about
those lines.

Is this well-defined according to the standard? I''ve read a little
about sequence points and not doing things like "i = i++". This
would seem to be similar. Is it only a problem if you''re using and
modifying the *same* variable, more than once, without an
intervening sequence point?

In particular, is

*p++ = *q++;

guaranteed to dereference p and q, and perform the assignment,
before either is incremented?

--------------------------------------------------
#include <stdio.h>

int main(void)
{
char s[] = "Some\ntext\nin\nseveral\nlines\n";
char t[100], *src, *dest;

// replace each LF with CRLF
for (src = s, dest = t; *src; ) {
if (*src == ''\n'')
*dest++ = ''\r'';
*dest++ = *src++;
}
*dest = ''\0'';

fputs(t, stdout);
return 0;
}
--------------------------------------------------

Thanks,
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo ''g******@cbobk.pbz'' | rot13

推荐答案

Tom Zych< tz ****** @ pobox.com>在新闻中写道:3F *************** @ pobox.com:
Tom Zych <tz******@pobox.com> wrote in news:3F***************@pobox.com:
嗨。我一直在使用这样的代码:

* p ++ =''x'';
* p ++ = * q ++;


很好。

现在很久了。有用。下面的代码编译没有
警告并正确运行。夹板也不会抱怨这些线条。


当然。

这是根据标准定义的吗?我读了一些关于序列点的内容,而没有做过像i = i ++这样的事情。这似乎是相似的。如果您在不使用
干预序列点的情况下使用和修改* same *变量,这只是一个问题吗?

特别是<

* p ++ = * q ++;

保证取消引用p和q,然后执行赋值,
再增加之前?
Hi. I''ve been using code like this:

*p++ = ''x'';
*p++ = *q++;
Fine.
for a long time now. It works. The code below compiles with no
warnings and runs correctly. Even splint doesn''t complain about
those lines.
Sure.
Is this well-defined according to the standard? I''ve read a little
about sequence points and not doing things like "i = i++". This
would seem to be similar. Is it only a problem if you''re using and
modifying the *same* variable, more than once, without an
intervening sequence point?

In particular, is

*p++ = *q++;

guaranteed to dereference p and q, and perform the assignment,
before either is incremented?




是的,这是明确定义的。必须计算* p和* q的值,并使用

作为分配。此外,p和q必须递增。这个

发生的顺序取决于编译器,但结果是明确定义的,是你期望的




-

- 马克 - >

-



Yes this is well defined. The values of *p and *q must be calculated and
used for the assigment. Also, p and q must be incremented. The order this
happens is up to the compiler but the result is well-defined and is what
you expect.

--
- Mark ->
--


Tom Zych写道:
Tom Zych wrote:
特别是,

* p ++ = * q ++;

保证取消引用p和q,然后执行赋值,
增加了吗?
In particular, is

*p++ = *q++;

guaranteed to dereference p and q, and perform the assignment,
before either is incremented?




号但是谁在乎呢?与i = i ++不同,上面的语句没有两次修改_same_位置的
。也就是说,除非p或q指向他们自己

(投射到appropritate类型) - 这将是一件奇怪的事情。


您的代码是很好。


好​​吧,我会使用const char src []和const char * s,或者只是使用

char * s =" Some \
text\\\
in\\\
several\\\
lines\\\
英寸;并且删除了src,这样当字符串文字支持

时,字符串文字最终会进入只读内存。和/ ** / comments而不是//注释如果代码是在B89编译器上编译的
。但这些是不同的问题。


-

Hallvard



No. But who cares? Unlike i = i++, the above statement doesn''t modify
the _same_ location twice. That is, unless p or q point to themselves
(cast to the appropritate type) - which would be a weird thing to do.

Your code is fine.

Well, I would have used const char src[] and const char *s, or just used
char *s = "Some\ntext\nin\nseveral\nlines\n"; and dropped src, so that
the string literal would end up in read-only memory when that is
supported. And /**/ comments instead of // comments if the code is
to be compiled on C89 compilers. But these are different issues.

--
Hallvard


Tom Zych< tz * *****@pobox.com>写道:
Tom Zych <tz******@pobox.com> wrote:
嗨。我一直在使用这样的代码:

* p ++ =''x'';
* p ++ = * q ++;

很长一段时间了。有用。下面的代码编译没有
警告并正确运行。夹板也不会抱怨这些线条。

这是根据标准定义的吗?
是的。

我读了一些关于序列点的内容,而没有做过像i = i ++这样的事情。这似乎是相似的。如果您在不使用
干预序列点的情况下多次使用和修改* same *变量,这只是一个问题吗?
确切地说。

特别是,

* p ++ = * q ++;

保证取消引用p和q,并执行分配,
之前是否递增?
是的。

------------------------------------ --------------
#include< stdio.h>

int main(void)
{
char s [] =" Some\xt\\\
in\\\
several\\\
lines\\\
" ;;
char t [100],* src,* dest;

//替换每个LF与CRLF
for(src = s,dest = t; * src;){
if(* src ==''\ n'')
* dest ++ ='' \'';
* dest ++ = * src ++;
}
* dest =''\ 0'';

fputs(t,stdout) ;
返回0;
}
--------------------------------- -----------------
看起来不错。

谢谢,
Hi. I''ve been using code like this:

*p++ = ''x'';
*p++ = *q++;

for a long time now. It works. The code below compiles with no
warnings and runs correctly. Even splint doesn''t complain about
those lines.

Is this well-defined according to the standard? Yes.
I''ve read a little
about sequence points and not doing things like "i = i++". This
would seem to be similar. Is it only a problem if you''re using and
modifying the *same* variable, more than once, without an
intervening sequence point? Exactly.

In particular, is

*p++ = *q++;

guaranteed to dereference p and q, and perform the assignment,
before either is incremented? Yes.

--------------------------------------------------
#include <stdio.h>

int main(void)
{
char s[] = "Some\ntext\nin\nseveral\nlines\n";
char t[100], *src, *dest;

// replace each LF with CRLF
for (src = s, dest = t; *src; ) {
if (*src == ''\n'')
*dest++ = ''\r'';
*dest++ = *src++;
}
*dest = ''\0'';

fputs(t, stdout);
return 0;
}
-------------------------------------------------- Looks good.

Thanks,




问候


Irrwahn

-

闭上眼睛,按三次逃生。



Regards

Irrwahn
--
Close your eyes and press escape three times.


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

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