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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:04:18+00:00 2026-05-27T08:04:18+00:00

In C++ it is possible to allocate a const object on heap : const

  • 0

In C++ it is possible to allocate a const object on heap:

const Class* object = new const Class();
const_cast<Class*>( object )->NonConstMethod(); // UB

so that attempt to write into an object will be UB.

I don’t get how such an object will be different from a heap-allocated object that is not declared const:

const Class* object = new Class();

I mean when I allocate an object on stack it goes to automatic storage which is implementation-specific and so there might be some implementation-specific means that would allow allocating const objects in some special way that would yield UB when I write to an object.

Yet whenever I use new the compiler is required to emit operator new() function invokation and that function can’t possibly do anything different – it just allocates memory in a uniform manner regardless of whether there was const in my code.

How is a const heap-allocated object different from a non-const one and how is undefined behavior possible if I try to modify it?

  • 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-27T08:04:19+00:00Added an answer on May 27, 2026 at 8:04 am

    There is no difference in the object. There is a difference in the (compile-time) type of the variable(s) used to refer to the memory area.

    This is semantic friction only: the variable is different, the actual memory used by the data bits is const/volatile agnostic.

    For a very amusing and enlightening story describing similar semantic friction see this all-time-favourite answer by Eric Lippert:

    •   Can a local variable's memory be accessed outside its scope?  

    On the Undefined Behaviour

    Treating const data in a non-const way can lead to Undefined Behaviour, because the compiler is allowed to do certain optimizations based on the knowledge that a const variable won’t change1. Changing it none-the-less (e.g. by const_cast<>) can lead to erronous results because the compiler’s assumptions are actively negated.

    1 Note that volatile is there to help in cases where const variables can get modified concurrently. You can see how const is a ‘local’ won’t/can’t touch promis, whereas volatile says: ‘don’t assume this won’t change, even though it is not being written to in this code segment’.
    `

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

Sidebar

Related Questions

Is it possible to write a C function that does the following? Allocate a
Is it possible to allocate an arbitrary memory block using the new operator? In
Possible Duplicate: const and global This code will produce error in c++ // Foo.cpp
In C++ a stack-allocated object can be declared const : const Class object; after
Possible Duplicate: C++: why is new needed? Why cant I use malloc to allocate
Is it possible to allocate locally-scoped memory in assembly? For example, consider the following
I'm curious about the following code: class MyClass { public: MyClass() : _myArray(new int[1024])
In a moment of madness, I decided to write a quadtree C++ template class.
I'm sure that it is possible but I just can't do it, which is:
I have some code that I had to write to replace a function 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.