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 8740515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:08:36+00:00 2026-06-13T11:08:36+00:00

#include <iostream> using namespace std; int main() { const int a = 2; const

  • 0
#include <iostream>

using namespace std;

int main() {
        const int a = 2;
        const int *p = &a;
        int *p2 = const_cast<int*>(p);
        *p2=5;
        char *p3 = (char *)&a;
        cout << "p2 is" << *p2 << endl;
        cout << "p2 address " << p2 << endl;
        cout << "a is " << a << endl;
        cout << "a address " << &a << endl;

        return 0;
}

Hi all!

According to the output, *p2 and a has different values, *p2 is 5 and a is 2.

However, p2 and &a are the same. I’m confused…

Could you please help me understand where this is the case?

Thank you very much!

  • 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-13T11:08:37+00:00Added an answer on June 13, 2026 at 11:08 am

    Undefined behavior means that anything can happen. Including this.

    5.2.11 Const cast [expr.const.cast]

    7) [ Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer
    to data member resulting from a const_cast that casts away a const-qualifier73 may produce undefined
    behavior (7.1.6.1). —end note ]

    The underlying reason might be that the compiler, seeing how a is const, optimizes cout << "a is " << a << endl; to a simple cout << "a is " << 2 << endl;.

    For example, even in a debug build, I get:

            cout << "a is " << a << endl;
    00CE1581  mov         esi,esp  
    00CE1583  mov         eax,dword ptr [__imp_std::endl (0CFD30Ch)]  
    00CE1588  push        eax  
    00CE1589  mov         edi,esp  
    //...
    00CE158B  push        2 
    //...
    00CE158D  push        offset string "a is " (0CE7840h)  
    00CE1592  mov         ecx,dword ptr [__imp_std::cout (0CFD308h)]  
    00CE1598  push        ecx  
    00CE1599  call        std::operator<<<std::char_traits<char> > (0CE1159h)  
    00CE159E  add         esp,8  
    00CE15A1  mov         ecx,eax  
    00CE15A3  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0CFD304h)]  
    00CE15A9  cmp         edi,esp  
    00CE15AB  call        @ILT+415(__RTC_CheckEsp) (0CE11A4h)  
    00CE15B0  mov         ecx,eax  
    00CE15B2  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0CFD300h)]  
    00CE15B8  cmp         esi,esp  
    00CE15BA  call        @ILT+415(__RTC_CheckEsp) (0CE11A4h)  
    

    I highlighted the essential part – 2 is pushed directly on the argument stack of operator<<, instead of the value of a being read.

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

Sidebar

Related Questions

The following code #include <iostream> using namespace std; int main() { const char* const
#include <iostream> using namespace std; int main (int argc, char* const argv[]) { int
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
#include <iostream> using namespace std; int main(void) { int num[5]; const int* &ref =
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <iomanip> using namespace std; int main() { char array[10]; for(int i
#include <iostream> using namespace std; class X { public: X() { cout<<Cons<<endl; } X(const
#include <iostream> using namespace std; struct Y; struct X { X(const Y&) { cout
#include <iostream> using namespace std; class A{ public: A() {std::cout<<A() ;} A(const A& a){cout<<A(const
Code #include <iostream> using namespace std; int main() { const int N = 22;

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.