문제

#include <stdio.h>
int main()
{
    const int a = 12;
    int *p;
    p = &a;
    *p = 70;
}

Will it work?

도움이 되었습니까?

해결책

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.

다른 팁

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.

Modifying a const qualified object through a pointer invokes undefined behaviour, and such is the result. It is likely to be something you'd expect though, ie. the previous value unchanged, if it has been placed in .text, etc.

It does indeed work with gcc. It didn't like it though:

test.c:6: warning: assignment discards qualifiers from pointer target type

But the value did change when executed. I won't point out the obvious no-no...

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.

Here the type of pointer p is int*, which is being assigned the value of type const int* (&a => address of a const int variable).

Implicit cast eliminates the constness, though gcc throws a warning (please note this largely depends on the implementation).

Since the pointer is not declared as a const, value can be changed using such pointer.

if the pointer would be declared as const int* p = &a, you won't be able to do *p = 70.

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.

Bad, BAD idea.

Also, the behavior is platform- and implementation-specific. If you're running on a platform where the constant is stored in non-writable memory, this obviously won't work.

And, why on earth would you want to? Either update the constant in your source, or make it a variable.

This code contains a constraint violation:

const int a = 12;
int *p;
p = &a;

The constraint violated is C11 6.5.16.1/1 "Simple assignment"; if both operands are pointers then the type pointed to by the left must have all the qualifiers of the type pointed to by the right. (And the types , sans qualifiers, must be compatible).

So the constraint is violated because &a has type const int *, which has const as a qualifier; but that qualifier does not appear in the type of p which is int *.

The compiler must emit a diagnostic and might not generate an executable. The behaviour of any executable would be completely undefined, since the program does not comply with the rules of the language.

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top