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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:44:46+00:00 2026-06-18T00:44:46+00:00

It is known that in C++ string literals are immutable and the result of

  • 0

It is known that in C++ string literals are immutable and the result of modifying a string literal is undefined. For example

char * str = "Hello!";
str[1] = 'a';

This will bring to an undefined behavior.

Besides that string literals are placed in static memory. So they exists during whole program. I would like to know why do string literals have such properties.

  • 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-18T00:44:48+00:00Added an answer on June 18, 2026 at 12:44 am

    There are a couple of different reasons.

    One is to allow storing string literals in read-only memory (as others have already mentioned).

    Another is to allow merging of string literals. If one program uses the same string literal in several different places, it’s nice to allow (but not necessarily require) the compiler to merge them, so you get multiple pointers to the same memory, instead of each occupying a separate chunk of memory. This can also apply when two string literals aren’t necessarily identical, but do have the same ending:

    char *foo = "long string";
    char *bar = "string";
    

    In a case like this, it’s possible for bar to be foo+5 (if I’d counted correctly).

    In either of these cases, if you allow modifying a string literal, it could modify the other string literal that happens to have the same contents. At the same time, there’s honestly not a lot of point in mandating that either — it’s pretty uncommon to have enough string literals that you could overlap that most people probably want the compiler to run slower just to save (maybe) a few dozen bytes or so of memory.

    By the time the first standard was written, there were already compilers that used all three of these techniques (and probably a few others besides). Since there was no way to describe one behavior you’d get from modifying a string literal, and nobody apparently thought it was an important capability to support, they did the obvious: said even attempting to do so led to undefined behavior.

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

Sidebar

Related Questions

String literals are array objects: typeid(hello).name() // char [6] This seems convenient because the
As we know, comparing two matching string literals can result in equality: hello ==
I have a string that contains a known number of double values. What's the
Possible Duplicate: String, StringBuffer, and StringBuilder We know that String are immutable where StringBuffer
Consider this javascript code: var s = Some string; s = More string; Will
I know that string literals in C/C++ have static storage duration , meaning that
I've read that multiline string literals were proposed to be added in Java 7.
Possible Duplicate: concatenation of two string literals Case 1 hello world (NO ERROR) Case
Even with int foo(char str[]); which will take in an array initialized to a
I need to egrep a string that isn't known before runtime and that I'll

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.