Two different values at the same memory address

前端 未结 7 789
春和景丽
春和景丽 2020-11-22 08:14

Code

#include 
using namespace std;

int main() {
    const int N = 22;
    int * pN = const_cast(&N);
    *pN = 33;
    co         


        
7条回答
  •  再見小時候
    2020-11-22 08:56

    int * pN = const_cast(&N);
    *pN = 33;
    

    Your code invokes Undefined Behavior1 because you are modifying the content of a const qualified variable/object.

    1) Undefined Behavior: Behavior, such as might arise upon use of an erroneous program construct or of erroneous data, for which the Standard imposes no requirements.[Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

提交回复
热议问题