Click to See Complete Forum and Search --> : Increasing capacity of a StringBuilder object
vikrampschauhan
February 3rd, 2005, 03:05 PM
What my understanding is -
Say we have an object of the StringBuilder class.
It will have some initial capacity - say 32
When we add the 33rd byte to the object, following steps happen:
1. A new StringBuilder object is allocated on the heap(with capacity 64 - double the initial capacity), and its reference returned
2. All the elements are copied from the old object to the new object
Whenever the capacity is increased, we have to go through the above mentioned overhead.
I believe I am correct?
Thanks
Vikram
darwen
February 3rd, 2005, 04:24 PM
Show example code - please in code blocks.
But in first assessment no, that doesn't happen. The reallocation of memory happens inside of the string builder class.
The capacity is its INITIAL SIZE.
Not it's ULTIMATE SIZE.
Besides the fact that the allocated memory in a string builder might not encompass an entire string.
Therefore try this to confirm :
const int StringSize = 2;
StringBuilder stringBuilder = new StringBuilder(StringSize);
stringBuilder.Append("Hello");
stringBuilder.Append(" ");
stringBuilder.Append("There");
string sString = stringBuilder.ToString();
Try this code block under the debugger with "StringSize" set to various values -
'sString' will still contain "Hello there" no matter what the value of "StringSize" is set to. StringBuilder is designed to add strings to strings without the need of a temporary string.
If you do :
string s1 = "Hello";
string s2 = " ";
string s3 = "There";
string sResult = s1 + s2 + s3;
the result is that a new string will be created which appends "s2" to "s3", and then another new string will be created to append "s1" to the result of the previous operation with "s2+s3" before it's assigned (by reference) to sResult.
Using the StringBuilder class these temporary strings don't exist, and therefore it's more efficient.
The StringBuilder class is also designed for interop between native and managed code in that you can determine the size of the string being passed back.
All of this information is available from MSDN...
Darwen.
cmiskow
February 4th, 2005, 04:03 PM
Actually Vikram, I believe you are mostly right. The initial capacity you specify is the amount of memory allocated for the string contained in the StringBuilder object. When you add to the object beyond the capacity, it does double its capacity automatically.
From MSDN:
"When you modify the StringBuilder, it does not reallocate size for itself until the capacity is reached. When this occurs, the new space is allocated automatically and the capacity is doubled."
The difference between this and what you posted is that there is not a new StringBuilder created, rather the storage mechanism internal to the StringBuilder is resized. Thus, you still have the same original StringBuilder object.
I do not know the exact mechanism for enlarging the internal capacity, but regardless there will be some overhead involved. This is why it is recommended to set the initial capacity as an estimate of the overall size you will need.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.