如何修改const变量的值? [英] How to modify value of const variable?

查看:576
本文介绍了如何修改const变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在gcc中修改 const 修改后的变量的值,但不能在其他编译器中修改。
我在gcc上试过这段代码,它更新了 i j (11)的值。使用一个在线编译器,我可以得到不同的值。

 #包括< stdio.h中> 
void main(){
const int i = 10;
int * j;
j =& i;
(* j)++;
printf(j的地址是i的%p地址是%p \ n,j,& i);
printf(我是%d,j是%d \ n,i,* j);
}


解决方案

是的,您可以做到这一点有点破解。

  #include< stdio.h> 
int main(){
const int a = 0;
*(int *)& a = 39;
printf(%d,a);在上面的代码中, a
$ / code>

$ c>是一个 const int 。随着小小的破解,你可以改变常数值。

更新:解释

在上面的代码中,a被定义为常量。例如,a有一个内存地址 0x01 ,因此& a 返回相同。当它被铸造成(int *)时,它变成另一个变量,被称为指向const的指针。当再次访问 * 时,可以在不违反const策略的情况下访问另一个变量,因为它不是原始变量,但因为它被引用作为指针的地址。

这可以在像Borland C ++或Turbo C ++这样的旧版本上运行,但是现在没有人会使用它。

这是未定义的行为,这意味着根据标准,您无法预测当您尝试此操作时会发生什么。它可能根据特定的机器,编译器和程序状态做不同的事情。



在这种情况下,最常发生的情况是答案是是。一个变量,const或不是,只是内存中的一个位置,你可以打破const的规则并简单地覆盖它。 (当然如果程序的其他部分取决于它的常量数据是不变的,这会导致严重的错误!)

然而在某些情况下 - 通常是常量静态数据 - 编译器可能会将这些变量放在内存的只读区域中。例如,MSVC通常会在可执行文件的.text段中放置常量静态整数,这意味着操作系统在尝试写入时会引发保护错误,程序将崩溃。



在编译器和机器的其他组合中,可能会发生完全不同的情况。你可以预见的一件事是,这种模式会激怒需要阅读代码的人。



试试这个,让我知道。


I am able to change the value of const modified variable in gcc but not in other compilers. I have tried this code on gcc, which updates the value of i and j (11). With an online compiler, I get different values.

#include<stdio.h>
void main() {
  const int i=10;
  int *j;
  j = &i;
  (*j)++;
  printf("address of j is %p address of i is %p\n",j,&i);
  printf("i is %d and j is %d\n",i,*j);
}

解决方案

Yes, you can do it with a little hack.

#include <stdio.h>
int main(){
    const int a = 0;
    *(int *)&a = 39;
    printf("%d", a);
}

In the above code, a is a const int. With the little hack, you can change the constant value.

Update: Explanation

In the above code, a is defined as a const. For example a has a memory addr 0x01 and therefore &a returns the same. When it is casted with (int *) it becomes another variable referred as a pointer to the const. When it is accessed with * again, the another variable can be accessed without violation of the const policy because it is not the original variable, but the changes are reflected because it is referred as address to the pointer.

This will work on older versions like Borland C++ or Turbo C++, however no one is using it now a days.

It's "undefined behaviour", meaning that based on the standard you can't predict what will happen when you try this. It may do different things depending on the particular machine, compiler, and state of the program.

In this case, what will most often happen is that the answer will be "yes". A variable, const or not, is just a location in memory, and you can break the rules of const and simply overwrite it. (Of course this will cause a severe bug if some other part of the program is depending on its const data being constant!)

However in some cases -- most typically for const static data -- the compiler may put such variables in a read-only region of memory. MSVC, for example, usually puts const static ints in .text segment of the executable, which means that the operating system will throw a protection fault if you try to write to it, and the program will crash.

In some other combination of compiler and machine, something entirely different may happen. The one thing you can predict for sure is that this pattern will annoy whoever has to read your code.

Try this and let me know.

这篇关于如何修改const变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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