gpt4 book ai didi

c - 为什么允许我们更改 "const"限定变量的值?为什么允许指针而不是赋值?

转载 作者:太空狗 更新时间:2023-10-29 15:17:59 29 4
gpt4 key购买 nike

考虑以下 2 个程序 prog1prog2。这里如果我尝试更改 const 限定变量的值 i 使用指针 ptr,我收到警告(不是错误)“初始化从指针目标类型中丢弃限定符|”,但程序仍然运行并显示新值。但是如果我尝试使用赋值语句在第二个程序中更改 i 的值,我会得到错误(不是警告)读取赋值- 仅变量“i”|

以下是由此前提引起的混淆:

1)为什么我们在任何情况下都允许更改只读 const 限定变量的值?这是否违背了使用 const 的目的限定符?如果我们尝试这样做,我们不应该得到一个错误吗?

2) 即使由于某些奇怪的原因我们被允许更改常量的值,为什么要区分使用指针更改只读 const 限定变量的值(这是允许的,带有警告)并通过使用赋值操作(这是不允许的并且给我们一个错误)?

//prog1
#include <stdio.h>

int main ()
{
const int i=8;
int *ptr=&i;
*ptr=9;
printf("%d",*ptr); //Prints new value nevertheless
}

警告:初始化丢弃指针目标类型的限定符|

//prog2
#include <stdio.h>

int main()
{
const int i=8;
i=10;
printf("%d",i);
}

错误:只读变量“i”的赋值|

编辑 H2CO3

这里我不止一次改变了const限定变量的值。我只得到一个警告,和prog1

中一样
//prog3
#include <stdio.h>

int main ()
{
const int i=8;
int *ptr=&i;
*ptr=9;
*ptr=10;
printf("%d",*ptr); //Prints 10
}

最佳答案

1) Why are we allowed to change the value of a read-only const qualified variable in any circumstance? Doesn't it defeat the purpose of using a const qualifier?

尝试通过赋值运算符更改 const 限定对象是违反约束的:

6.5.16 在约束下:

2 An assignment operator shall have a modifiable lvalue as its left operand.

可修改的左值在 6.3.2.1 (1) 中定义:

A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

作为约束违规,它需要编译器根据 5.1.1.3 (1) 提供诊断消息:

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.

但是不需要实现来拒绝无效程序,因此诊断消息也可以是警告而不是错误。

但是,通过没有 const 限定类型的左值修改声明为 const 的对象不是约束违规,尽管它会调用未定义的行为,6.7.3 (6):

If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

由于它既不是约束违规也不是无效语法,因此甚至不需要发出诊断消息。

Shouldn't we get an error if we attempt to do so?

如果您尝试通过具有 const 限定类型的左值修改对象,您必须得到诊断消息。

由于严重违反了声明的意图,大多数编译器在这些情况下都会发出错误。

如果您尝试通过具有非 const 限定类型的左值来修改具有 const 限定类型的对象,如下所示

const int i=8;
int *ptr=&i;
*ptr=9;

尝试通过表达式 *ptr = 9 修改 i 会调用未定义的行为,但这不是约束违规(或语法错误),因此不需要诊断信息(没有给出)。

初始化时会发出诊断消息

int *ptr = &i;

因为根据 6.5.16.1 (1),这又是一个约束违规:

One of the following shall hold:

  • the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;
  • the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right;
  • the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
  • the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
  • the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or
  • the left operand has type atomic, qualified, or unqualified _Bool, and the right is a pointer.

然而,该诊断通常是警告而不是错误,因为人们可能会显式地放弃 const

int *ptr = (int*)&i;

而不能从 i 中丢弃 const

通过指向非 const 限定对象类型的指针修改对象是有效的如果指向的对象是可修改的。愚蠢的例子:

int i = 8;
const int *cptr = &i; // valid, no problem adding const
int *mptr = (int*)cptr;
*mptr = 9; // no problem, pointee is non-const

2) Even if due to some strange reason we are allowed to change values of constants, why the discrimination between changing the value of a read-only const qualified variable using a pointer (which is allowed,with a warning) and through using an assignment operation (which is simply not allowed and gives us an error)?

直接分配给具有 const 限定类型的对象不仅违反约束,而且明显违反规定的语义。声明一个对象 const 明确表示“我不想修改该对象”。

通过指向非 const 限定类型的指针修改对象不是约束违规,只有当指针具有 const 限定类型时才会出现未定义行为。允许将指向 const 限定类型的指针转​​换为指向对应的非 const 限定类型的指针,并且通过该指针修改指针对象可能是有效的,因此您只会收到警告,并且仅当未进行转换时明确的。

在给定的简短示例中,编译器可以检测到指针对象具有 const 限定类型,因此修改会调用未定义的行为,但通常很难检测到,而且通常无法检测到。因此,编译器甚至不尝试检测简单情况,不值得付出努力。

关于c - 为什么允许我们更改 "const"限定变量的值?为什么允许指针而不是赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16542817/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com