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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:32:04+00:00 2026-05-18T08:32:04+00:00

Consider the following C++ program: #include <cstdlib> // for exit(3) #include <string> #include <iostream>

  • 0

Consider the following C++ program:

#include <cstdlib> // for exit(3)
#include <string>
#include <iostream>
using namespace std;

void die()
{
    exit(0);
}

int main()
{
    string s("Hello, World!");
    cout << s << endl;
    die();
}

Running this through valgrind shows this (some output trimmed for brevity):

==1643== HEAP SUMMARY:
==1643==     in use at exit: 26 bytes in 1 blocks
==1643==   total heap usage: 1 allocs, 0 frees, 26 bytes allocated
==1643==
==1643== LEAK SUMMARY:
==1643==    definitely lost: 0 bytes in 0 blocks
==1643==    indirectly lost: 0 bytes in 0 blocks
==1643==      possibly lost: 26 bytes in 1 blocks
==1643==    still reachable: 0 bytes in 0 blocks
==1643==         suppressed: 0 bytes in 0 blocks

As you can see, there’s a possibility that 26 bytes allocated on the heap were lost. I know that the std::string class has a 12-byte struct (at least on my 32-bit x86 arch and GNU compiler 4.2.4), and “Hello, World!” with a null terminator has 14 bytes. If I understand it correctly, the 12-byte structure contains a pointer to the character string, the allocated size, and the reference count (someone correct me if I’m wrong here).

Now my questions: How are C++ strings stored with regard to the stack/heap? Does a stack object exist for a std::string (or other STL containers) when declared?

P.S. I’ve read somewhere that valgrind may report a false positive of a memory leak in some C++ programs that use STL containers (and “almost-containers” such as std::string). I’m not too worried about this leak, but it does pique my curiosity regarding STL containers and memory management.

  • 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-18T08:32:04+00:00Added an answer on May 18, 2026 at 8:32 am

    Others are correct, you are leaking because you are calling exit. To be clear, the leak isn’t the string allocated on the stack, it is memory allocated on the heap by the string. For example:

    struct Foo { };
    
    int main()
    {
        Foo f;
        die();
    }
    

    will not cause valgrind to report a leak.

    The leak is probable (instead of definite) because you have an interior pointer to memory allocated on the heap. basic_string is responsible for this. From the header on my machine:

       *  A string looks like this:
       *
       *  @code
       *                                        [_Rep]
       *                                        _M_length
       *   [basic_string<char_type>]            _M_capacity
       *   _M_dataplus                          _M_refcount
       *   _M_p ---------------->               unnamed array of char_type
       *  @endcode
       *
       *  Where the _M_p points to the first character in the string, and
       *  you cast it to a pointer-to-_Rep and subtract 1 to get a
       *  pointer to the header.
    

    They key is that _M_p doesn’t point to the start of the memory allocated on the heap, it points to the first character in the string. Here is a simple example:

    struct Foo
    {
        Foo()
        {
            // Allocate 4 ints.
            m_data = new int[4];
            // Move the pointer.
            ++m_data;
            // Null the pointer
            //m_data = 0;
        }
        ~Foo()
        {
            // Put the pointer back, then delete it.
            --m_data;
            delete [] m_data;
        }
        int* m_data;
    };
    
    int main()
    {
        Foo f;
        die();
    }
    

    This will report a probable leak in valgrind. If you comment out the lines where I move m_data valgrind will report ‘still reachable’. If you uncomment the line where I set m_data to 0 you’ll get a definite leak.

    The valgrind documentation has more information on probable leaks and interior pointers.

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

Sidebar

Related Questions

Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;
Consider the following small program: #include <cstdio> int main() { printf(%d\n, 1); std::printf(%d\n, 2);
Lets us consider the following program : #include <stdlib.h> int main(int argc, char **argv){
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)

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.