常数 - pp与const [英] constants - pp vs. const

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

问题描述

我记得在某处读过C'''const''关键字几乎没用,

除了可能触发一些额外的编译器警告。 ''无用''作为

in:一个符合标准的C编译器不能假设一个声明为const的变量是实际常量(从未修改过)
,因为它是有效的const

修饰符,例如


const int foo = 2;

int * bar;


bar =(int *)& foo;


* bar = 4;


这是正确的吗?

I remember reading somewhere that C''s ''const'' keyword is almost useless,
except for maybe triggering some additional compiler warnings. ''Useless'' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?

推荐答案



" copx" < co ** @ gazeta.plschrieb im Newsbeitrag

news:f4 ********** @ inews.gazeta.pl ...

"copx" <co**@gazeta.plschrieb im Newsbeitrag
news:f4**********@inews.gazeta.pl...

>我记得在哪里读过C'''const''关键字几乎没用,
>I remember reading somewhere that C''s ''const'' keyword is almost useless,



[snip]


对不起,错误的标题。我原本打算发布一个不同的问题和

忘了改变主题。

[snip]

Sorry, wrong header. I originally intended to post a different question and
forgot to change the subject.


copx写于06/11/07 11:10, :
copx wrote On 06/11/07 11:10,:

我记得在哪里读过C'''const''关键字几乎没用,

除了可能触发一些额外的编译器警告。 ''无用''作为

in:一个符合标准的C编译器不能假设一个声明为const的变量是实际常量(从未修改过)
,因为它是有效的const

修饰符,例如


const int foo = 2;

int * bar;


bar =(int *)& foo;


* bar = 4;


这是正确的吗?
I remember reading somewhere that C''s ''const'' keyword is almost useless,
except for maybe triggering some additional compiler warnings. ''Useless'' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?



否,任何修改const限定变量的尝试

都会产生未定义的行为。实际上,你会发现一些编译器会在只读内存中分配const限定的

变量。


至于几乎没用的另一方面,我拒绝参加空头辩论,与邋mor的蠢货进行空谈:

使用加载的条款来推进他们不朽的议程。


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


" copx" < co ** @ gazeta.plwrote:
"copx" <co**@gazeta.plwrote:

我记得在某个地方读过C'''const''关键字几乎没用,

除了可能触发一些额外的编译器警告。 ''无用''作为

in:一个符合标准的C编译器不能假设一个声明为const的变量是实际常量(从未修改过)
,因为它是有效的const

修饰符,例如


const int foo = 2;

int * bar;


bar =(int *)& foo;


* bar = 4;


这是正确的吗?
I remember reading somewhere that C''s ''const'' keyword is almost useless,
except for maybe triggering some additional compiler warnings. ''Useless'' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?



不完全。这样做会导致未定义的行为。 C实现_is_

允许假设const _object_保持不变。你在想什么是b $ b的另一端:给定


const int * bar;


一个实现不能假设bar处的对象将保持
常量,因为bar可以指向一个本身不是
const的对象。 IOW,_pointers to_ const类型没有它们可以用的那么有用

。但是,它们对功能参数仍然非常有用;对于

的例子,给出声明


void func(const int * baz);


两者的实现并且维护程序员可以依赖

func()而不是改变baz所指向的对象,至少不是通过

baz,并且提供了func()。这经常用在

标准库中;例如,strcpy()被声明


char * strcpy(char * s1,const char * s2);


这意味着如果你正确调用strcpy(),你可以依赖它

永远不会修改你传递它的第二个字符串,无论它对

第一个字符串做什么。 (在C99中,使用限制来收紧if和

得到的保证。)


Richard

Not quite. Doing so causes undefined behaviour. A C implementation _is_
allowed to assume that a const _object_ remains constant. What you''re
thinking of is the other end of the equation: given

const int *bar;

an implementation cannot assume that the object at bar will remain
constant, because bar can be pointed at an object that itself is not
const. IOW, _pointers to_ const types are not as useful as they could
be. However, they''re still very useful for function parameters; for
example, given the declaration

void func(const int *baz);

both the implementation and the maintenance programmer can rely on
func() not changing the object that baz points at, at least not through
baz, and provided func() is well written. This is frequently used in the
Standard library; for example, strcpy() is declared

char *strcpy(char *s1, const char *s2);

and this means that if you call strcpy() correctly, you can rely on it
never modifying the second string you pass it, whatever it does to the
first string. (In C99, restrict is used to tighten both that "if" and
the resulting guarantee.)

Richard


这篇关于常数 - pp与const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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