Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:39:52+00:00 2026-06-02T06:39:52+00:00

In C, I’m working on a class that manages a byte buffer, allowing arbitrary

  • 0

In C, I’m working on a “class” that manages a byte buffer, allowing arbitrary data to be appended to the end. I’m now looking into automatic resizing as the underlying array fills up using calls to realloc. This should make sense to anyone who’s ever used Java or C# StringBuilder. I understand how to go about the resizing. But does anyone have any suggestions, with rationale provided, on how much to grow the buffer with each resize?

Obviously, there’s a trade off to be made between wasted space and excessive realloc calls (which could lead to excessive copying). I’ve seen some tutorials/articles that suggest doubling. That seems wasteful if the user manages to supply a good initial guess. Is it worth trying to round to some power of two or a multiple of the alignment size on a platform?

Does any one know what Java or C# does under the hood?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:39:54+00:00Added an answer on June 2, 2026 at 6:39 am

    In C# the strategy used to grow the internal buffer used by a StringBuilder has changed over time.

    There are three basic strategies for solving this problem, and they have different performance characteristics.

    The first basic strategy is:

    • Make an array of characters
    • When you run out of room, create a new array with k more characters, for some constant k.
    • Copy the old array to the new array, and orphan the old array.

    This strategy has a number of problems, the most obvious of which is that it is O(n2) in time if the string being built is extremely large. Let’s say that k is a thousand characters and the final string is a million characters. You end up reallocating the string at 1000, 2000, 3000, 4000, … and therefore copying 1000 + 2000 + 3000 + 4000 + … + 999000 characters, which sums to on the order of 500 billion characters copied!

    This strategy has the nice property that the amount of “wasted” memory is bounded by k.

    In practice this strategy is seldom used because of that n-squared problem.

    The second basic strategy is

    • Make an array
    • When you run out of room, create a new array with k% more characters, for some constant k.
    • Copy the old array to the new array, and orphan the old array.

    k% is usually 100%; if it is then this is called the “double when full” strategy.

    This strategy has the nice property that its amortized cost is O(n). Suppose again the final string is a million characters and you start with a thousand. You make copies at 1000, 2000, 4000, 8000, … and end up copying 1000 + 2000 + 4000 + 8000 … + 512000 characters, which sums to about a million characters copied; much better.

    The strategy has the property that the amortized cost is linear no matter what percentage you choose.

    This strategy has a number of downside that sometimes a copy operation is extremely expensive, and you can be wasting up to k% of the final string length in unused memory.

    The third strategy is to make a linked list of arrays, each array of size k. When you overflow an existing array, a new one is allocated and appended to the end of the list.

    This strategy has the nice property that no operation is particularly expensive, the total wasted memory is bounded by k, and you don’t need to be able to locate large blocks in the heap on a regular basis. It has the downside that finally turning the thing into a string can be expensive as the arrays in the linked list might have poor locality.

    The string builder in the .NET framework used to use a double-when-full strategy; it now uses a linked-list-of-blocks strategy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.