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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:12:44+00:00 2026-05-26T21:12:44+00:00

I am working on a project with existing code which uses mainly C++ but

  • 0

I am working on a project with existing code which uses mainly C++ but with c-style strings. Take the following:

#include <iostream>
int main(int argc, char *argv[])
{
    char* myString = "this is a test";
    myString = "this is a very very very very very very very very very very very long string";
    cout << myString << endl;
    return 0;
}

This compiles and runs fine with the output being the long string.

However I don’t understand WHY it works. My understanding is that

char* myString 

is a pointer to an area of memory big enough to hold the string literal “this is a test”. If that’s the case, then how am I able to then store a much longer string in the same location? I expected it to crash when doing this due to trying to cram a long string into a space set aside for the shorter one.

Obviously there’s a basic misunderstanding of what’s going on here so I appreciate any help understanding this.

  • 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-26T21:12:45+00:00Added an answer on May 26, 2026 at 9:12 pm

    You’re not changing the content of the memory, you’re changing the value of the pointer to point to a different area of memory which holds "this is a very very very very very very very very very very very long string".

    Note that char* myString only allocates enough bytes for the pointer (usually 4 or 8 bytes). When you do char* myString = "this is a test";, what actually happened was that before your program even started, the compiler allocated space in the executable image and put "this is a test" in that memory. Then when you do char* myString = "this is a test"; what it actually does is just allocate enough bytes for the pointer, and make the pointer point to that memory it had already allocated at compile time, in the executable.

    So if you like diagrams:

    char* myString = "this is a test";
    
    (allocate memory for myString)
    
                  ---> "this is a test"
                / 
    myString---
    
                       "this is a very very very very very very very very very very very long string"
    

    Then

    myString = "this is a very very very very very very very very very very very long string";
    
                       "this is a test"
    
    myString---
                \
                  ---> "this is a very very very very very very very very very very very long string"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on an existing project I suddenly got the following error when trying
I am working on a project to take an existing project using JSF1.2, JSP,
I am currently working on a project which uses the AutoFac Inversion of Control
I'm currently working on a project which is using EF Code First with POCOs.
I'm currently working on a project which is using EF Code First with POCOs.
I'm attempting to get google-toolbox-for-mac working with my existing XCode 3.2 iPad project, but
I am working on a project which uses a relational database (SQL Server 2008).
I am working on an existing project, setup by another coder. I'm having some
I am working on implementing Zend Framework within an existing project that has a
I'm working on a project to convert an existing Java web application to use

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.