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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:13:39+00:00 2026-05-11T06:13:39+00:00

I have following questions regarding strings in C++: 1>> which is a better option(considering

  • 0

I have following questions regarding strings in C++:

1>> which is a better option(considering performance) and why?

1.

string a; a = "hello!"; 

OR

2.

string *a; a = new string("hello!"); ... delete(a); 

2>>

string a; a = "less";  a = "moreeeeeee";  

how exactly memory management is handled in c++ when a bigger string is copied into a smaller string? Are c++ strings mutable?

  • 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. 2026-05-11T06:13:39+00:00Added an answer on May 11, 2026 at 6:13 am

    All the following is what a naive compiler would do. Of course as long as it doesn’t change the behavior of the program, the compiler is free to make any optimization.

    string a; a = 'hello!'; 

    First you initialize a to contain the empty string. (set length to 0, and one or two other operations). Then you assign a new value, overwriting the length value that was already set. It may also have to perform a check to see how big the current buffer is, and whether or not more memory should be allocated.

    string *a; a = new string('hello!'); ... delete(a); 

    Calling new requires the OS and the memory allocator to find a free chunk of memory. That’s slow. Then you initialize it immediately, so you don’t assign anything twice or require the buffer to be resized, like you do in the first version. Then something bad happens, and you forget to call delete, and you have a memory leak, in addition to a string that is extremely slow to allocate. So this is bad.

    string a; a = 'less';  a = 'moreeeeeee'; 

    Like in the first case, you first initialize a to contain the empty string. Then you assign a new string, and then another. Each of these may require a call to new to allocate more memory. Each line also requires length, and possibly other internal variables to be assigned.

    Normally, you’d allocate it like this:

    string a = 'hello'; 

    One line, perform initialization once, rather than first default-initializing, and then assigning the value you want.

    It also minimizes errors, because you don’t have a nonsensical empty string anywhere in your program. If the string exists, it contains the value you want.

    About memory management, google RAII. In short, string calls new/delete internally to resize its buffer. That means you never need to allocate a string with new. The string object has a fixed size, and is designed to be allocated on the stack, so that the destructor is automatically called when it goes out of scope. The destructor then guarantees that any allocated memory is freed. That way, you don’t have to use new/delete in your user code, which means you won’t leak memory.

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

Sidebar

Related Questions

I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I'm writing a small webapp in Grails and I have the following question regarding
I have a question regarding the following code snippet I came across in one
Following on from my recent question regarding parsing XML files in Java I have
I've started using Enterprise Library and have the following questions: 1)How do I add
I have been repeatedly asked the following questions in many interviews.... But still can't
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,
Regarding this post and this other one . Suppose I have the following: public
I have a questions.xml file which has a list of questions for my quiz,

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.