strings allocate in heap or stack?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: strings allocate in heap or stack?

  1. #1
    Join Date
    Sep 2005
    Posts
    336

    strings allocate in heap or stack?

    Hi
    When we define a string and then get user his/her name , what is happening in memory.I mean:
    Code:
        string s;
        cout << "Enter name";
        cin >>s;
    Here s is a ponter and it is in the stack and points to name data which user enter is in the heap.Is this true?If wrong please correct it.
    Thanks...

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: strings allocate in heap or stack?

    The string object exists on the stack but it will contain pointers to areas of memory in "free store" (often called the heap although they are not technically the same- string has an allocator parameter which you usually ignore but can use a free-store other than the regular heap).

  3. #3
    Join Date
    Apr 2006
    Location
    Nr Cambridge, UK
    Posts
    263

    Re: strings allocate in heap or stack?

    std::string encapsulates its characters, managing the memory for them automatically. In your code, s is not a pointer, but a std::string variable. Whether the string uses "heap" or "stack" memory to hold the characters is an implementation detail, and in fact, some implementations use the stack for short strings (say, up to 16 characters) and the heap for longer strings (this is called the Small String Optimisation, or SSO). You don't need to know this to use std::string, except that it can help you to understand its performance characteristics.

  4. #4
    Join Date
    May 2006
    Posts
    327

    Re: strings allocate in heap or stack?

    Quote Originally Posted by NMTop40
    The string object exists on the stack but it will contain pointers to areas of memory in "free store" (often called the heap although they are not technically the same- string has an allocator parameter which you usually ignore but can use a free-store other than the regular heap).
    Please note that in new STL (Visual Studio 2003 and 2005), the std::string class uses a combined variant of allocating strings. If the length is long then the string is allocated in heap area, but if it is short, it is stored in a preallocated area of the class, i.e. in a data member declared as "char s[...]". (That's why sizeof(std::string) returns an apparently too big value: 32).

    This is done for performance reasons. Whe should take this into account when we work with collections of strings.

    So we can say std::string allocates short strings on the stack, but long ones -- on the heap.

  5. #5
    Join Date
    Sep 2005
    Posts
    336

    Re: strings allocate in heap or stack?

    Thanks friends but there is one thing that i don't understand if it is stored in stack.I know that if any os allocate memory in stack it must know its size.But for example if we say:
    Code:
    string s;
        cout << "Enter name";
        cin >>s;
    here s's size is unknown so how can it be?How can string class allocate memory in stack that unknown size?If this is not possible it seems that string data must be allocated always in heap.Is this true?

    I am looking for your answers.
    Thanks...

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: strings allocate in heap or stack?

    Quote Originally Posted by Viorel
    Please note that in new STL (Visual Studio 2003 and 2005), the std::string class uses a combined variant of allocating strings. If the length is long then the string is allocated in heap area, but if it is short, it is stored in a preallocated area of the class, i.e. in a data member declared as "char s[...]". (That's why sizeof(std::string) returns an apparently too big value: 32).

    This is done for performance reasons. Whe should take this into account when we work with collections of strings.

    So we can say std::string allocates short strings on the stack, but long ones -- on the heap.
    Actually I use the same implementatino method for my portable_string class. With mine the cut-off length is 16.

    In STL, when it uses free-storage at all, it is done with the allocator class, not by std::string itself.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,424

    Re: strings allocate in heap or stack?

    Quote Originally Posted by sawer
    Thanks friends but there is one thing that i don't understand
    The source code for the std::string class should be available to you. Since all of what you are asking is implementation defined, it would be easier and more direct if you debug this yourself to see what happens.

    Otherwise you shouldn't worry about how std::string does whatever it does. It works, just use it.
    Code:
    string s;
        cout << "Enter name";
        cin >>s;
    here s's size is unknown
    It is well known. The size is sizeof(std::string). Or are you referring to the data that it may hold?
    How can string class allocate memory in stack that unknown size?
    Again, run this program under a debugger, and the answer for your particular implementation should be known to you.

    In general, "how does the standard library class do this" type questions can easily be answered by just debugging a simple application that uses the class. Since the source code is usually all in header files, you have your answers right there, either by inspecting the source code, and/or debugging a running program.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Sep 2005
    Posts
    336

    Re: strings allocate in heap or stack?

    Quote Originally Posted by Paul McKenzie
    It is well known. The size is sizeof(std::string). Or are you referring to the data that it may hold?
    Yes.I don't say object's, or refernces or pointers size.Of course its size is known.Anyway i understand that string class do so much more than i think and it allocate data in heap; delete it automatically.I hope i don't understand it wrongly.

    thanks for all answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center