none
strings allocate in stack or heap? RRS feed

  • Question

  • 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;
    İf allocation always in heap:
    Here s is a ponter and it is in the stack and points to name data which user enter is in the heap.

    If allocation in stack:
    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.

    Are these true?
    I aml ooking for your answers.
    Thanks
    Friday, June 16, 2006 2:59 PM

Answers

  • 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 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.

     

    In the original sample, if the user enter a short string, (shorter than 16 characters), then the string will be stored in a buffer within s, i.e. on the stack. Otherwise the string will be allocated on the heap, and s will keep pointers to that zone.

    Friday, June 16, 2006 3:39 PM
  • That's quite commendable.  Note that you have the full source code for the string class on your PC.  It's kinda tough to read but it is all there.  Open c:\program files\microsoft visual studio 8\vc\include\string.  Single-step through your source code with the debugger to get an idea on how it works.

    Saturday, June 17, 2006 12:27 AM
    Moderator

All replies

  • The string class object is stored on the stack.  The contents of the string is stored in the heap.

    Friday, June 16, 2006 3:23 PM
    Moderator
  • 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 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.

     

    In the original sample, if the user enter a short string, (shorter than 16 characters), then the string will be stored in a buffer within s, i.e. on the stack. Otherwise the string will be allocated on the heap, and s will keep pointers to that zone.

    Friday, June 16, 2006 3:39 PM
  • Thanks nobugz and  viorel  

    viorel can you give me please link for this information.

    Thanks again
    Good works..
    Friday, June 16, 2006 7:47 PM
  • The point is you should not care how the memory is allocated. The string takes care of everything (like all the STL containers) so that you don't have to do that.
    Friday, June 16, 2006 9:22 PM
  • Thanks Marius but i must know this.I want to be a system programmer.I must know every step in memory.This question is not beginner question, yes it seems like that.But not.
    Friday, June 16, 2006 10:44 PM
  • That's quite commendable.  Note that you have the full source code for the string class on your PC.  It's kinda tough to read but it is all there.  Open c:\program files\microsoft visual studio 8\vc\include\string.  Single-step through your source code with the debugger to get an idea on how it works.

    Saturday, June 17, 2006 12:27 AM
    Moderator
  •  sawer wrote:

    viorel can you give me please link for this information.

    I don’t have a link for this information. I only analyzed the implementation of basic_string class from xstring file and debugged some simple programs.

     

    The implementation can change from version to version.

     

    Monday, June 19, 2006 6:18 AM