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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:10:14+00:00 2026-05-22T12:10:14+00:00

If I have a constant variable, is it stored in a seperate memory space

  • 0

If I have a constant variable, is it stored in a seperate memory space from non-constant variables? I have encounter some odd in this program.

    //--------assign a const value to non-const value-------
const int cst_a = 5;
int* ptra = const_cast<int*>(&cst_a);

cout<<&cst_a<<" "<<ptra<<endl; // same address

*ptra = 6;

cout<<*ptra<<" "<<cst_a<<endl; // same address with different value

//--------assign a non-const value to const value-------

int b = 50;
const int* cst_ptr_b = &b;

cout<<cst_ptr_b<<" "<<&b<<endl; // also same address

b = 55;
cout<<b<<" "<<*cst_ptr_b<<endl; // same address with same value

return 0;

In the first case, &cst_a and ptra has the same memory address, but their value can change seperately. In the second case, cst_ptr_b and &b are also same address, but their value change symetrically. Why?

  • 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-05-22T12:10:15+00:00Added an answer on May 22, 2026 at 12:10 pm

    It may be stored in a memory area that can’t be modified. Because of this, your const_cast results in undefined behavior.

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

Sidebar

Related Questions

is this possible: changing a constant variable to non-constant I am making a whole
I have a Class named Constants that contains all the constant variable in my
I have a base class A with a constant static variable a. I need
We have this constant discussion in our project as to the granularity of our
I have a Module with a constant and variable. I wonder how I could
So I have an issue with using a constant variable in the following switch
I am curious how PHP handles variables in memory? If I have 100 constants
I have a string stored in variable say $input = 999 success: id:10.123/AVC13231 |
I have some global settings variables I need to store for my application, but
I have a Java program performing some large analysis tasks, running through a loop

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.