943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 492
  • C++ RSS
Nov 7th, 2009
0

std::string capacity(). Different in different OSs?!?!

Expand Post »
Hi, I have a question.

Using the following code:

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main()
  3. {
  4. std::string s1;
  5. std::cout << s1.capacity() << std::endl;
  6.  
  7. system ("PAUSE");
  8. return 0;
  9. }

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.
Reputation Points: 41
Solved Threads: 0
Junior Poster in Training
Carrots is offline Offline
62 posts
since Mar 2009
Nov 7th, 2009
0
Re: std::string capacity(). Different in different OSs?!?!
The string member capacity() is defined as:
Quote ...
Return size of allocated storage
Returns the size of the allocated storage space in the string object.

Notice that the capacity is not necessarily equal to the number of characters that conform the content of the string (this can be obtained with members size or length), but the capacity of the allocated space, which is either equal or greater than this content size.

Notice also that this capacity does not suppose a limit to the length of the string. If more space is required to accomodate content in the string object, the capacity is automatically expanded, or can even be explicitly modified by calling member reserve.

The real limit on the size a string object can reach is returned by member max_size.
It is possible to speculate that different operating systems employ different memory management strategies. It is also possible to speculate that the same operating system will handle memory allocation differently among different machines. To go one step further, perhaps the same operating system will allocate memory differently at times on the same machine (depending on what processes are running, available memory etc.)

A lot of variables at play here to come up with a rock-solid answer. Since OS design is usually company-top-secret, perhaps we'll never know exactly how one OS handles memory allocation in comparison to others.
Last edited by Clinton Portis; Nov 7th, 2009 at 11:05 am.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 7th, 2009
2
Re: std::string capacity(). Different in different OSs?!?!
The capacity of a container class has nothing to do with the OS, it's an internal mechanism of the C++ implementation. In other words, capacity will potentially return a different value for every single implementation of the standard library you use because the author of each library chooses how to manage memory growth for the container.

Now the max_size member function for containers is more likely to depend on the OS.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Grabbing a reference to or pointer to...
Next Thread in C++ Forum Timeline: building sphinx4 helloworld in netbeans - errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC