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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:06:47+00:00 2026-05-27T20:06:47+00:00

int main() { const int maxint=100;//The program will crash if this line is put

  • 0
int main()
{
    const int maxint=100;//The program will crash if this line is put outside the main
    int &msg=const_cast<int&>(maxint);  
    msg=200;  
    cout<<"max:"<<msg<<endl; 
    return 0;
}

The function will run ok if the ‘const int maxint=100;’ definition is put inside the main function but crash and popup a error message said “Access Violation” if put outside.

Someone says it’s some kind of ‘undefined behavior’, and i want to know the exact answer and how i can use the const cast safely?

  • 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-27T20:06:47+00:00Added an answer on May 27, 2026 at 8:06 pm

    They are correct, it is undefined behaviour. You’re not allowed to modify the value of a const variable, which is the danger of casting away the constness of something: you better know it’s not really const.

    The compiler, seeing that maxint is const and should never be modified, doesn’t even have to give it an address. It can just replace all the uses of maxint with 100 if it sees fit. Also it might just put the constant in to a portion of memory that is read-only, as Matteo Italia points out, which is probably what’s happening for you. That’s why modifying it produces undefined behaviour.

    The only way you can safely cast away the constness of a variable is if the variable is not actually const, but the const qualifier was added to a non-const variable, like this:

    int f(const int& x) {
        int& nonconst = const_cast<int&>(x);
    
        ++nonconst;
    }
    
    int blah = 523;
    
    f(blah); // this is safe
    
    const int constblah = 123;
    
    f(constblah); // this is undefined behaviour
    

    Think about this example, which compiles perfectly:

    int f(const int& x) {
        int& nonconst = const_cast<int&>(x);
    
        ++nonconst;
    }
    
    int main() {
        f(4); // incrementing a number literal???
    }
    

    You can see how using const_cast is pretty dangerous because there’s no way to actually tell whether a variable is originally const or not. You should avoid using const_cast when possible (with functions, by just not accepting const parameters).

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

Sidebar

Related Questions

I just translated this program, #include <stdio.h> int dam[1000][1000]; int main (int argc, const
Check out this code #include<stdio.h> int main() { const int a=7; int *p=&a; (*p)++;
#include<stdio.h> int main() { const int sum=100; int *p=(int *)&sum; *p=101; printf(%d, %d,*p,sum); return
int main() { int p; scanf(%d,&p); fun() { int arr[p]; // isn't this similar
I don't know why this simple code is not working: int main() { const
This is my code: int main() { const int LEN = 5; int x[LEN];
#include <iostream> int main() { const int i=10; int *p =(int *) &i; *p
The following code #include <iostream> using namespace std; int main() { const char* const
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int main() { int *d=0; printf(%d\n,*d); return 0; } this works fine. >cc legal.c

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.