space in string

This is a discussion on space in string within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, The space in string should use heap address memory space, not stack, right? But through debugging, for example, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    space in string

    Hello everyone,


    The space in string should use heap address memory space, not stack, right? But through debugging, for example,

    Code:
    string str = "hello";
    why I can not see the invocation of new operator? Anyone could point out where STL string class allocates space on heap and using which function to allocate please (any other approach other than using new to allocate space on heap?)?


    thanks in advance,
    George

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    That's an interesting question. I don't know which function std::string calls to allocate memory. I could only guess to the best of my ability which wouldn't get you very far. I (and everyone else) could probably help you more if I knew why you were looking for std::string to call the new operator in the first place. And yes, there are other ways to allocate memory besides using the new operator.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Some string implementations keep a small buffer (like maybe 16 bytes) for short strings to avoid dynamically allocating small buffers.

    Try it with a string more than 16 characters. If that doesn't work, keep adding to the string in a loop, eventually it will have to allocate memory. If that doesn't work, you aren't trapping the allocation properly.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you look through the <string> header file (& whatever implementation specific headers it includes) I'm sure you'll find some places where it calls new. Probably deep in the bowels of the STL...

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, you won't. std::string only calls its allocator's allocate() function. You'll have to look for std::allocator to find the new call.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course, even if you find the answer in THIS particular implementation, that will not necessarily reflect some other variant of std::string implementation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks everyone,


    I find Daved and CornedBee are correct. The related code is in xmemory and xstring. I want to share my findings with you here and let you comment if you think I am correct or not. :-)

    1. For small buffer, will use 16-bytes stack array;
    2. For large buffer, will use allocator to allocate space on heap.

    Here are the related code in class string,

    Code:
    	union _Bxty
    		{	// storage for small buffer or pointer to larger one
    		_Elem _Buf[_BUF_SIZE];
    		_Elem *_Ptr;
    		} _Bx;
    Quote Originally Posted by CornedBee View Post
    No, you won't. std::string only calls its allocator's allocate() function. You'll have to look for std::allocator to find the new call.

    regards,
    George

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Please note that that information is really only useful if we know what compiler and standard library you are using, since implementations vary (especially for the string class).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    It's Microsoft Visual C++. 2008 I think?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Daved and Elysia,


    Quote Originally Posted by c View Post
    It's Microsoft Visual C++. 2008 I think?
    Yes, it is Visual Studio 2008. I like to try new things.


    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21