Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:17:04+00:00 2026-06-08T10:17:04+00:00

Possible Duplicate: Modifying a const through a non-const pointer lets’ see the code below

  • 0

Possible Duplicate:
Modifying a const through a non-const pointer

lets’ see the code below first.very short code:

int main()
{
    const int n=9;
    int *p=(int*)&n;
    *p=5;
    cout<<*p<<endl; //result is 5
    cout<<n<<endl;  // 9
    int a=n+1;
    cout<<a<<endl;  // 10
}

To my surprise,the compiler doesn’t complain at all.And the result is as what i showed as comment.
But,if that makes sense more or less.What’s more astonishing is that,
when you debug it, you can see that the value of n has actually changed to 5!
But then ,when you use “n”,it seemed that the “n” is still treated as the original value 9!
So i guess the compiler has stored the original value of “n” in somewhere else, right?
Then where is the original value of “n”,which is 9, stored now?
Thank you for all your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:17:05+00:00Added an answer on June 8, 2026 at 10:17 am

    This probably due to optimizations. Since you declare const int n=9, you’re basically making a promise that you won’t modify n. So the compiler is free to optimize cout << n << endl; to a simple cout << 9 << endl;. I don’t think the old value is stored anywhere else.

    Nevertheless, this is undefined behavior.

    I can confirm that the cause of this behavior is optimization, even on a debug build:

        cout<<n<<endl;  
    01151426  mov         esi,esp  
    01151428  mov         eax,dword ptr [__imp_std::endl (11582B4h)]  
    0115142D  push        eax  
    0115142E  mov         edi,esp  
    
    //HERE:
    //
    01151430  push        9  
    //
    //
    
    01151432  mov         ecx,dword ptr [__imp_std::cout (11582B0h)]  
    01151438  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11582ACh)]  
    0115143E  cmp         edi,esp  
    01151440  call        @ILT+325(__RTC_CheckEsp) (115114Ah)  
    01151445  mov         ecx,eax  
    01151447  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11582A8h)]  
    0115144D  cmp         esi,esp  
    0115144F  call        @ILT+325(__RTC_CheckEsp) (115114Ah)  
    

    n doesn’t even come into discussion.

    If n wasn’t const, the optimization would be illegal:

    00D51426  mov         esi,esp  
    00D51428  mov         eax,dword ptr [__imp_std::endl (0D582B4h)]  
    00D5142D  push        eax  
    00D5142E  mov         edi,esp  
    
    //HERE
    //
    00D51430  mov         ecx,dword ptr [n]  
    00D51433  push        ecx  
    //
    //
    
    00D51434  mov         ecx,dword ptr [__imp_std::cout (0D582B0h)]  
    00D5143A  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0D582ACh)]  
    00D51440  cmp         edi,esp  
    00D51442  call        @ILT+325(__RTC_CheckEsp) (0D5114Ah)  
    00D51447  mov         ecx,eax  
    00D51449  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0D582A8h)]  
    00D5144F  cmp         esi,esp  
    00D51451  call        @ILT+325(__RTC_CheckEsp) (0D5114Ah)  
    

    Here, the value of n is pushed on the argument stack because it’s not const.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Modifying a const through a non-const pointer I have the following code:
Possible Duplicate: Modifying a const through a non-const pointer I'm studying C++, and very
Possible Duplicate: Modifying C string constants? Pointer to const char vs char array vs
Possible Duplicate: Modifying .NET Dictionary while Enumerating through it I have a dictionary object
Possible Duplicate: Getting a value from HttpServletRequest.getRemoteUser() in Tomcat without modifying application Short version:
Possible Duplicate: Modifying value of char pointer in c produces segfault This is a
Possible Duplicate: ruby code for modifying outer quotes on strings? This is my string:
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Modifying C string constants? As far as I understand if you want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.