Can we change the value of an object defined with const through pointers?

前端 未结 10 1221
北恋
北恋 2020-11-22 10:49
#include 
int main()
{
    const int a = 12;
    int *p;
    p = &a;
    *p = 70;
}

Will it work?

相关标签:
10条回答
  • 2020-11-22 11:10

    It's undefined behaviour. Proof:

    /* program.c */
    
    int main()
    {
            const int a = 12;
            int* p;
            p = &a;
            *p = 70;
            printf("%d\n", a);
            return 0;
    }
    
    
    gcc program.c
    

    and run it. Output will be 70 (gcc 4.3)

    Then compile it like this:

    gcc -O2 program.c
    

    and run it. The output will be 12. When it does optimisation, the compiler presumably loads 12 into a register and doesn't bother to load it again when it needs to access a for the printf because it "knows" that a can't change.

    0 讨论(0)
  • 2020-11-22 11:11

    yes, you can make it done by using such code. but the code do not apply when when a is global (a gcc-compiled program gave me segmentation fault.)

    generally speaking, in beloved C, you can almost always find someway to hack things that are not supposed to be changed or exposed. const here being a example.

    But thinking about the poor guy(maybe myself after 6 months) maintains our code, I often choose not do so.

    0 讨论(0)
  • 2020-11-22 11:16

    Modifying a const qualified object through a pointer invokes undefined behaviour, and such is the result. It may be something you'd expect from a particular implementation, e.g. the previous value unchanged, if it has been placed in .text, etc.

    0 讨论(0)
  • 2020-11-22 11:18

    Yes, you can change the value of a constant variable.
    Try this code:

    #include <stdio.h>
    
    int main()
    {
        const  int x=10;
    
        int *p;
        p=(int*)&x;
        *p=12;
        printf("%d",x);
    }
    
    0 讨论(0)
  • 2020-11-22 11:21

    You cannot change the value of a constant variable by using a pointer pointing to it. This type of pointer is called as Pointer to a constant.

    There is also another concept called Constant Pointer. It means that once a pointer points to a memory location you cannot make it point to the another location.

    0 讨论(0)
  • 2020-11-22 11:22

    It's "undefined behavior," 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 constness 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.

    0 讨论(0)
提交回复
热议问题