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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 22
Reputation: Carrots is an unknown quantity at this point 
Solved Threads: 0
Carrots Carrots is offline Offline
Newbie Poster

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

 
0
  #1
17 Days Ago
Hi, I have a question.

Using the following code:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 287
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 28
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz in Training
 
0
  #2
17 Days Ago
The string member capacity() is defined as:
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; 17 Days Ago at 11:05 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
0
  #3
17 Days Ago
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC