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

  • Home
  • SEARCH
  • 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 7999781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:36:04+00:00 2026-06-04T15:36:04+00:00

Possible Duplicate: Modifying a const through a non-const pointer I’m studying C++, and very

  • 0

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

I’m studying C++, and very interesting about pointers. And I tried to change value of a constant value (my teacher called it backdoor, please clarify if I’m wrong) like this:

const int i = 0;

const int* pi = &i;

int hackingAddress = (int)pi;
int *hackingPointer = (int*)pi;

*hackingPointer = 1;

cout << "Address:\t" << &i << "\t" << hackingPointer << endl;
cout << "Value:  \t" << i << "\t\t" << *hackingPointer << endl;

system("PAUSE");

return 0;

However, the result is very strange. Although the two addresses are the same, the values are different.

How is my code executed? And where is 0 and 1 value is stored exactly?

  • 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-04T15:36:07+00:00Added an answer on June 4, 2026 at 3:36 pm

    You’ve discovered a little thing that C++ developers call undefined behavior. You told the compiler that “i is a constant with the value 0″. So when you ask the compiler for the value of i, it tells you that it is 0.

    Mucking around with trying to change the value of a constant violates the assumptions made by the compiler (that constants are going to be, well, constant), and so, the compiler is going to generate invalid or inconsistent code.

    There are a lot of situations in C++ where it is possible to do something without the compiler catching it as an error, but the result is undefined. And if you do that, then you get results like what you’re seeing. The compiler does something weird and unexpected.

    Oh, and if your teacher is trying to teach you anything from an example such as this, he’s wrong, and you should be very scared.

    The only guarantee you get from code like this is this:

    the compiler can do literally anything it likes

    When you write code, you have an implicit contract with the compiler:

    “If I write well-defined C++ code, then you convert it into an executable with the same effects as described by the C++ standard”.

    When you do something like this, you violate the contract. And then the compiler isn’t obliged to follow it either. If you give the compiler code that is not well-defined according to the C++ standard, then it can’t, and isn’t going to, create an executable which does as the C++ standard specifies.

    • 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 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: Modifying value of char pointer in c produces segfault This is a
Possible Duplicate: Modifying C string constants? As far as I understand if you want
Possible Duplicate: How to change database settings for a deployed war file? OK I
Possible Duplicate: Getting a value from HttpServletRequest.getRemoteUser() in Tomcat without modifying application Short version:
Possible Duplicate: Why is Perl foreach variable assignment modifying the values in the array?
Possible Duplicate: Modifying C string constants? can you explain why i get access violation
Possible Duplicate: Disassembling, modifying and then reassembling a Linux executable I've been told that

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.