C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2009, 08:32 PM   #1
Registered User
 
Join Date: Aug 2007
Location: U.K.
Posts: 127
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:

Quote:
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!
Swerve is offline   Reply With Quote
Old 11-06-2009, 08:59 PM   #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   Reply With Quote
Old 11-06-2009, 11:06 PM   #3
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
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
iMalc is offline   Reply With Quote
Old 11-07-2009, 05:22 AM   #4
Registered User
 
Join Date: Oct 2008
Posts: 452
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.
EVOEx is offline   Reply With Quote
Old 11-07-2009, 06:00 AM   #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:
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).
anon is offline   Reply With Quote
Old 11-07-2009, 08:26 AM   #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 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.
Swerve is offline   Reply With Quote
Old 11-07-2009, 08:45 AM   #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:
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).

Last edited by anon; 11-07-2009 at 08:47 AM.
anon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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