C/C++ changing the value of a const

后端 未结 18 1693
醉梦人生
醉梦人生 2020-11-28 07:28

I had an article, but I lost it. It showed and described a couple of C/C++ tricks that people should be careful. One of them interested me but now that I am trying to repli

相关标签:
18条回答
  • 2020-11-28 07:33

    In C++, Using Microsoft Visual Studio-2008

    const int a = 3;    /* I promisse i won't change a */
    int * ptr1  = const_cast<int*> (&a);
    *ptr1 = 5;  /* I'm a liar, a is now 5 . It's not okay. */
    cout << "a = " << a << "\n"; /* prints 3 */
    int arr1[a]; /* arr1 is an array of 3 ints */
    
    int temp = 2;
    /* or, const volatile int temp = 2; */
    const int b = temp + 1; /* I promisse i won't change b */
    int * ptr2  = const_cast<int*> (&b);
    *ptr2 = 5; /* I'm a liar, b is now 5 . It's okay. */
    cout << "b = " << b << "\n"; /* prints 5 */
    //int arr2[b]; /* Compilation error */
    

    In C, a const variable can be modified through its pointer; however it is undefined behavior. A const variable can be never used as length in an array declaration.

    In C++, if a const variable is initialized with a pure constant expression, then its value cannot be modified through its pointer even after try to modify, otherwise a const variable can be modified through its pointer.

    A pure integral const variable can be used as length in an array declaration, if its value is greater than 0.

    A pure constant expression consists of the following operands.

    1. A numeric literal (constant ) e.g. 2, 10.53

    2. A symbolic constant defined by #define directive

    3. An Enumeration constant

    4. A pure const variable i.e. a const variable which is itself initialized with a pure constant expression.

    5. Non-const variables or volatile variables are not allowed.

    0 讨论(0)
  • 2020-11-28 07:34

    this will create a runtime fault. Because the int is static. Unhandled exception. Access violation writing location 0x00035834.

    void main(void)
    {
        static const int x = 5;
        int *p = (int *)x;
        *p = 99;                //here it will trigger the fault at run time
    }
    
    0 讨论(0)
  • 2020-11-28 07:35

    we can change the const variable value by the following code :

    const int x=5; 
    
    printf("\nValue of x=%d",x);
    
    *(int *)&x=7;
    
    printf("\nNew value of x=%d",x);
    
    0 讨论(0)
  • 2020-11-28 07:35
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void) {
        const int a = 1; //a is constant
        fprintf(stdout,"%d\n",a);//prints 1
        int* a_ptr = &a;
        *a_ptr = 4;//memory leak in c(value of a changed)
        fprintf(stdout,"%d",a);//prints 4
    return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 07:36

    You probably want to use const_cast:

    int *ptr = const_cast<int*>(ptr_to_a);
    

    I'm not 100% certain this will work though, I'm a bit rusty at C/C++ :-)

    Some readup for const_cast: http://msdn.microsoft.com/en-us/library/bz6at95h(VS.80).aspx

    0 讨论(0)
  • 2020-11-28 07:38

    The article you were looking at might have been talking about the difference between

    const int *pciCantChangeTarget;
    const int ci = 37;
    pciCantChangeTarget = &ci; // works fine
    *pciCantChangeTarget = 3; // compile error
    

    and

    int nFirst = 1;
    int const *cpiCantChangePointerValue = &nFirst;
    int nSecond = 968;
    
    *pciCantChangePointerValue = 402; // works
    cpiCantChangePointerValue = &ci; // compile error
    

    Or so I recall-- I don't have anything but Java tools here, so can't test :)

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