![]() |
| | #1 | |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 127
| Buffer size question. 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;
}
Quote:
I'm unsure if I'm missing the point somewhat. Many thanks! | |
| Swerve is offline | |
| | #2 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| 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%. |
| grumpy is offline | |
| | #3 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| Quote:
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 | |
| iMalc is offline | |
| | #4 | |
| Registered User Join Date: Oct 2008
Posts: 452
| Quote:
So if I know up front the length of the resulting string, I tend to reserve the data before actually doing anything with it. | |
| EVOEx is offline | |
| | #5 | |
| The larch Join Date: May 2006
Posts: 3,082
| 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. Quote:
| |
| anon is offline | |
| | #6 |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 127
| 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 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. |
| Swerve is offline | |
| | #7 | |
| The larch Join Date: May 2006
Posts: 3,082
| 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.)
__________________ I might be wrong. Quote:
Last edited by anon; 11-07-2009 at 08:47 AM. | |
| anon is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Get file size when in buffer | Siphon | C++ Programming | 10 | 04-10-2008 10:52 AM |
| Converting a circulating mouse pointer to use a Potentionmeter | phoenix23 | C Programming | 16 | 10-29-2006 05:04 AM |
| Trouble with DMA Segmentation Faults | firestorm717 | C Programming | 2 | 05-07-2006 09:20 PM |
| Question on buffer overflows | maxhavoc | C++ Programming | 3 | 11-25-2004 03:48 PM |
| Quick C Question - checking buffer for input | sean | C Programming | 3 | 11-13-2004 12:23 PM |