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

  • Home
  • SEARCH
  • 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 6067673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:38:00+00:00 2026-05-23T09:38:00+00:00

I stumbled upon Stack Overflow question Memory leak with std::string when using std::list<std::string> ,

  • 0

I stumbled upon Stack Overflow question Memory leak with std::string when using std::list<std::string>, and one of the comments says this:

Stop using new so much. I can’t see any reason you used new anywhere you did. You can create objects by value in C++ and it’s one of the huge advantages to using the language. You do not have to allocate everything on the heap. Stop thinking like a Java programmer.

I’m not really sure what he means by that.

Why should objects be created by value in C++ as often as possible, and what difference does it make internally? Did I misinterpret the answer?

  • 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-23T09:38:01+00:00Added an answer on May 23, 2026 at 9:38 am

    There are two widely-used memory allocation techniques: automatic allocation and dynamic allocation. Commonly, there is a corresponding region of memory for each: the stack and the heap.

    Stack

    The stack always allocates memory in a sequential fashion. It can do so because it requires you to release the memory in the reverse order (First-In, Last-Out: FILO). This is the memory allocation technique for local variables in many programming languages. It is very, very fast because it requires minimal bookkeeping and the next address to allocate is implicit.

    In C++, this is called automatic storage because the storage is claimed automatically at the end of scope. As soon as execution of current code block (delimited using {}) is completed, memory for all variables in that block is automatically collected. This is also the moment where destructors are invoked to clean up resources.

    Heap

    The heap allows for a more flexible memory allocation mode. Bookkeeping is more complex and allocation is slower. Because there is no implicit release point, you must release the memory manually, using delete or delete[] (free in C). However, the absence of an implicit release point is the key to the heap’s flexibility.

    Reasons to use dynamic allocation

    Even if using the heap is slower and potentially leads to memory leaks or memory fragmentation, there are perfectly good use cases for dynamic allocation, as it’s less limited.

    Two key reasons to use dynamic allocation:

    • You don’t know how much memory you need at compile time. For instance, when reading a text file into a string, you usually don’t know what size the file has, so you can’t decide how much memory to allocate until you run the program.

    • You want to allocate memory which will persist after leaving the current block. For instance, you may want to write a function string readfile(string path) that returns the contents of a file. In this case, even if the stack could hold the entire file contents, you could not return from a function and keep the allocated memory block.

    Why dynamic allocation is often unnecessary

    In C++ there’s a neat construct called a destructor. This mechanism allows you to manage resources by aligning the lifetime of the resource with the lifetime of a variable. This technique is called RAII and is the distinguishing point of C++. It "wraps" resources into objects. std::string is a perfect example. This snippet:

    int main ( int argc, char* argv[] )
    {
        std::string program(argv[0]);
    }
    

    actually allocates a variable amount of memory. The std::string object allocates memory using the heap and releases it in its destructor. In this case, you did not need to manually manage any resources and still got the benefits of dynamic memory allocation.

    In particular, it implies that in this snippet:

    int main ( int argc, char* argv[] )
    {
        std::string * program = new std::string(argv[0]);  // Bad!
        delete program;
    }
    

    there is unneeded dynamic memory allocation. The program requires more typing (!) and introduces the risk of forgetting to deallocate the memory. It does this with no apparent benefit.

    Why you should use automatic storage as often as possible

    Basically, the last paragraph sums it up. Using automatic storage as often as possible makes your programs:

    • faster to type;
    • faster when run;
    • less prone to memory/resource leaks.

    Bonus points

    In the referenced question, there are additional concerns. In particular, the following class:

    class Line {
    public:
        Line();
        ~Line();
        std::string* mString;
    };
    
    Line::Line() {
        mString = new std::string("foo_bar");
    }
    
    Line::~Line() {
        delete mString;
    }
    

    Is actually a lot more risky to use than the following one:

    class Line {
    public:
        Line();
        std::string mString;
    };
    
    Line::Line() {
        mString = "foo_bar";
        // note: there is a cleaner way to write this.
    }
    

    The reason is that std::string properly defines a copy constructor. Consider the following program:

    int main ()
    {
        Line l1;
        Line l2 = l1;
    }
    

    Using the original version, this program will likely crash, as it uses delete on the same string twice. Using the modified version, each Line instance will own its own string instance, each with its own memory and both will be released at the end of the program.

    Other notes

    Extensive use of RAII is considered a best practice in C++ because of all the reasons above. However, there is an additional benefit which is not immediately obvious. Basically, it’s better than the sum of its parts. The whole mechanism composes. It scales.

    If you use the Line class as a building block:

     class Table
     {
          Line borders[4];
     };
    

    Then

     int main ()
     {
         Table table;
     }
    

    allocates four std::string instances, four Line instances, one Table instance and all the string’s contents and everything is freed automagically.

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

Sidebar

Related Questions

So I'm trying to install a web application and I stumbled upon this question:
I stumbled upon this question from two years ago. Is there a way to
Update : I just stumbled upon this in Eric Lippert's answer to another question
I stumbled upon that word in this blog post . There is similar question
Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL
I'm using callables quite often , and I've stubled upon a question that irritates
Question: I just stumbled upon: http://icculus.org/~chunky/writing/inetd I realized it removes the http://www in front
This should be a fun puzzle for you Stack Overflow geniuses: I'm building a
I stumbled upon this piece of code while reading about DES encryption. I wonder
I stumbled upon this website: http://www.liptongreenmint.ro/ I like their small and simple slideshow with

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.