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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:27:43+00:00 2026-06-02T15:27:43+00:00

It maybe seems to be a silly question but i really need to clarify

  • 0

It maybe seems to be a silly question but i really need to clarify this:

Will this bring any danger to my program?

Is the const_cast even needed?

If i change the input pointers values in place will it work safely with std::string or will it create undefined behaviour?

So far the only concern is that this could affect the string “some_text” whenever I modify the input pointer and makes it unusable.

std::string some_text = "Text with some input";

char * input = const_cast<char*>(some_text.c_str());

Thanks for giving me some hints, i would like to avoid the shoot in my own foot

  • 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-02T15:27:44+00:00Added an answer on June 2, 2026 at 3:27 pm

    As an example of evil behavior: the interaction with gcc’s Copy On Write implementation.

    #include <string>
    #include <iostream>
    
    int main() {
        std::string const original = "Hello, World!";
        std::string copy = original;
    
        char* c = const_cast<char*>(copy.c_str());
        c[0] = 'J';
    
        std::cout << original << "\n";
    }
    

    In action at ideone.

    Jello, World!

    The issue ? As the name implies, gcc’s implementation of std::string uses a ref-counted shared buffer under the cover. When a string is modified, the implementation will neatly check if the buffer is shared at the moment, and if it is, copy it before modifying it, ensuring that other strings sharing this buffer are not affected by the new write (thus the name, copy on write).

    Now, with your evil program, you access the shared buffer via a const-method (promising not to modify anything), but you do modify it!

    Note that with MSVC’s implementation, which does not use Copy On Write, the behavior would be different ("Hello, World!" would be correctly printed).

    This is exactly the essence of Undefined Behavior.

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

Sidebar

Related Questions

Maybe this question is silly but I really don't know how to solve. First,
This question might seem really silly to most of the enlightened folks here. But
OK, I know this question seems pretty easy to answer, or maybe documented, but
I'm not a ruby expert and may be this will seem a silly question...but
This may be a really silly question, but I've found no answer on google/bing...
I'm newer in T-SQL.So maybe my question will seem naive. I need to retrieve
Hopefully this is a silly question but my brain is fried and I can't
Ok, this may be a silly question, maybe my English knowledge, or just my
Sorry if my question seems to be silly, but I cannot find something relate
I know this may sound silly but it seems that, my version of TortoiseSVN

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.