| | |
std::string capacity(). Different in different OSs?!?!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 22
Reputation:
Solved Threads: 0
Hi, I have a question.
Using the following code:
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.
Using the following code:
c++ Syntax (Toggle Plain Text)
#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.
0
#2 17 Days Ago
The string member capacity() is defined as:
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.
•
•
•
•
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.
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.
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.
Now the max_size member function for containers is more likely to depend on the OS.
I'm here to prove you wrong.
![]() |
Similar Threads
- cannot convert parameter 1 from 'std::string' to 'char *' (C++)
- assigning a TCHAR char array to std::string (C++)
- TCHAR to std::string (C)
- first cannot conver std::string to const char, now runtime error! (C++)
- converting std::string to const chars? (C++)
- no matching function for call to 'strcmp(std::string&, std::string&)' (C++)
- C++ - std::string input (C++)
Other Threads in the C++ Forum
- Previous Thread: Grabbing a reference to or pointer to...
- Next Thread: building sphinx4 helloworld in netbeans - errors
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






