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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:22:32+00:00 2026-06-10T19:22:32+00:00

I’ve decided to come back to C++ after some time spent in Java and

  • 0

I’ve decided to come back to C++ after some time spent in Java and now I’m quite confused about how strings work in C++.

To start with, suppose we have a function:

void fun() {
   int a = 1;
   Point b(1,2);
   char c[] = "c-string";
}

As I understand, a and b are allocated on the stack. c (the pointer) is allocated on the stack too, but the contents (“c-string”) live happily on the heap.

Q1: Are the contents of c automatically deallocated when the function fun ends?

Secondly let’s suppose we have a c++ string:

void fun2() {
  (1) string s = "c++ string";
  (2) s += "append";
  (3) s = "new contents";
  (4) s = "a" + s + "c";
}

String documentation isn’t too specific about how the strings work, so here are the questions:

Q2: Are the contents of s automatically deallocated after fun2 ends?

Q3: What does happen when we concatenate two strings? Should I care about memory usage? (line 2)

Q4: What happens when we overwrite the contents of a string (line 3) – what about memory, should I worry? Is the originally allocated space reused?

Q5: What if I construct a string like this (line 4). Is it expensive? Are string literals ("a","c") pooled (like in Java) or repeated throughout the final executable?

What I am ultimately trying to learn is how to correctly use strings in C++.

Thanks for reading this,
Queequeg

  • 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-10T19:22:33+00:00Added an answer on June 10, 2026 at 7:22 pm

    As I understand, a and b are allocated on the stack. c (the pointer) is allocated on the stack too, but the contents (“c-string”) live happily on the heap.

    That’s wrong, they all live in automatic memory (the stack). Even the char array. In C++, a string is an object of type std::string.

    Q1: Are the contents of c automatically deallocated when the function fun ends?

    Yes.

    Q2: Are the contents of s automatically deallocated after fun2 ends?

    Yes.

    Q3: What does happen when we concatenate two strings? Should I care about memory usage? (line 2)

    They are concatenated, and the memory is managed automatically. (assuming we’re talking about std::string and not char[] or char*.

    Q4: What happens when we overwrite the contents of a string (line 3) – what about memory, should I worry? Is the originally allocated space reused?

    Implementation detail. It can be reused, it can be re-allocated if the previous memory can’t hold the new contents.

    Q5: What if I construct a string like this (line 4). Is it expensive? Are string literals (“a”,”c”) pooled (like in Java) or repeated throughout the final executable?

    String literals can be pooled, but it’s not required. For large concatenation, it’s usual to use a std::stringstream instead (similar to Java). But profile first, don’t do premature optimizations. Not that neither of those are string literals though.

    char* pStr = "this is a string literal";
    

    This resides in read-only memory and can’t be modified.

    What I am ultimately trying to learn is how to correctly use strings in C++.

    Use a std::string.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am currently running into a problem where an element is coming back from

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.