Buffer size question.

This is a discussion on Buffer size question. within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to find the size of the buffer for an uninitialized string and an empty string, but ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Buffer size question.

    Hi,

    I am trying to find the size of the buffer for an uninitialized string and an empty string, but using size() and length() doesn't seem to be helping me.

    Here is the program I am using to try and find the size of the buffers being used for the strings:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string s1;  //unitialized string using size()
    	cout << s1.size() << endl;
    
    	string s2 = ("");  //empty string using size()
    	cout << s2.size() << endl;
    
    	string s3;  //unitialized string using length()
    	cout << s3.length() << endl;
    
    	string s4 = ("");  //empty string using length()
    	cout << s4.length() << endl;
    
    	system ("PAUSE");
    	return 0;
    }
    Which is giving the following output:

    0
    0
    0
    0
    sh: PAUSE: not found
    Press Enter to continue!
    Could anyone tell me how I can find the size of the buffers being used?

    I'm unsure if I'm missing the point somewhat.

    Many thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    size() and length() return the length of the buffer that is actually being used. Since a std::string records the actual length of the content, there is no need for terminating characters in the buffer. That is why you will be seeing a string string initialised with "" ending up with a size() of zero.

    If you want the length of the internally allocated buffer, use capacity(). Change capacity() manually using reserve().
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by Swerve View Post
    Could anyone tell me how I can find the size of the buffers being used?

    I'm unsure if I'm missing the point somewhat.
    What makes you think that in those examples the buffers are necessarily greater than zero anyway?

    You're not allowed to write into a string past its length, so finding the buffer size is of dubious use anyway
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by iMalc View Post
    You're not allowed to write into a string past its length, so finding the buffer size is of dubious use anyway
    The only reason I can think of for finding the buffer size is optimization. If you know you're going to append dozens of strings it might be a lot better first to reserve enough data so that no appending actually has to allocate data. Otherwise it might have to re-allocate a lot of times.
    So if I know up front the length of the resulting string, I tend to reserve the data before actually doing anything with it.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The comments that you have an uninitialized string vs empty string are also wrong. The default constructor initializes the string object to represent an empty string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks for all the information chaps. Learning a lot.

    I have a question.

    Using the following code:

    Code:
    #include <iostream>
    int main()
    {
    	std::string s1;
    	std::cout << s1.capacity() << std::endl;
    
    	system ("PAUSE");
    	return 0;
    }
    In Ubuntu/KDevelop the output is 0.

    In Windows/V.Studio the output is 15.

    Can anyone suggest why it happens?

    I'm somewhat stumped by it's occurrence.

    Thanks very much, much appreciated.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is entirely implementation-dependent. The capacity must be at least as much as the size of the string: it could be more, and often is (because the string also probably needs to meet some complexity requirements, when it grows), but doesn't have to be at any time.

    If VC++ implementation uses a short-string optimization (short strings are stored on the stack), I suppose it is impossible for it to have zero capacity.

    (Also include <string> for string class, and <cstdlib> for the system function.)
    Last edited by anon; 11-07-2009 at 07:47 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get file size when in buffer
    By Siphon in forum C++ Programming
    Replies: 10
    Last Post: 04-10-2008, 10:52 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Question on buffer overflows
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 02:48 PM
  5. Quick C Question - checking buffer for input
    By sean in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 11:23 AM

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