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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:03:45+00:00 2026-06-16T08:03:45+00:00

Possible Duplicate: Is a string literal in c++ created in static memory? If I

  • 0

Possible Duplicate:
Is a string literal in c++ created in static memory?

If I do:
const char* StringPtr = "string0",
then it is definitely somewhere in the memory, and I can get the address of StringPtr.

But if I do:
#define STRING0 "string0", then where does STRING0 reside?
Or, is STRING0 not existing in memory because compiler replace using of STRING0 by "string0"?

As far as I’ve known, whenever you write any string in your code, compiler must put it somewhere in the memory, but I don’t know the exact behavior of it.
But I am not very sure about this.

Can anyone explain how strings that are #define-ed or declared as char* are manipulated by the compiler?

Also, which one is better? To #define, extern const char* or extern const std::stringin the header file for strings?

Thanks!

  • 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-16T08:03:47+00:00Added an answer on June 16, 2026 at 8:03 am

    In almost all cases, the compiler is allowed to put a string literal wherever it wants. There might be one copy for each time the literal appears in source code, or one master copy shared among the instances.

    This causes trouble sometimes in C where const doesn’t mean the same thing and you are allowed to modify the memory. On one platform all the identical strings get changed, while on another changes don’t propagate. As of C++11 string literals don’t implicitly lose constness and the mistake is harder to make.

    The strings will all be initialized before the program starts, so in effect they are part of the executable binary image. That much is certain.

    What would be different is this:

    const char StringPtr[] = "string0",
    

    This defines a dedicated array object with a unique address.

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

Sidebar

Related Questions

Possible Duplicate: Is a string literal in c++ created in static memory? C++ string
Possible Duplicate: String literals vs const char* in C Why i can do this?
Possible Duplicate: In C++, Why can't I write to a string literal while I
Possible Duplicate: C/C++, can you #include a file into a string literal? I have
Possible Duplicate: Sizeof string literal On my windows7 mingw environment, I tried this: char
Possible Duplicate: Can a string literal be subscripted in a constant expression? If I
Possible Duplicate: Modifying C string constants? Pointer to const char vs char array vs
Possible Duplicate: Can I convert a C# string value to an escaped string literal
Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: Calling NSLog from C++: “Format string is not a string literal (potentially

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.