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 9100497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:52:52+00:00 2026-06-17T00:52:52+00:00

I am working on a Windows C project which is string-intensive: I need to

  • 0

I am working on a Windows C project which is string-intensive: I need to convert a marked up string from one form to another. The basic flow is something like:

DWORD convert(char *point, DWORD extent)
{
   char *point_end = point + extent;
   char *result = memory_alloc(1);
   char *p_result = result;

   while (point < point_end)
   {
      switch (*point)
      {
        case FOO:
          result_extent = p_result - result;
          result = memory_realloc(12);
          result += result_extent;
          *p_result++ = '\n';
          *p_result++ = '\t';
          memcpy(result, point, 10);
          point += 10;
          result += 10;
          break;
        case BAR:
          result_extent = p_result - result;
          result = memory_realloc(1);
          result += result_extent;
          *result++ = *point++;
          break;          
        default:
          point++;
          break;
      }
   }

   // assume point is big enough to take anything I would copy to it
   memcpy(point, result, result_extent);

   return result_extent;
}

memory_alloc() and memory_realloc() are fake functions to highlight the purpose of my question. I do not know beforehand how big the result ‘string’ will be (technically, it’s not a C-style/null-terminate string I’m working with, just a pointer to a memory address and a length/extent), so I’ll need to dynamically size the result string (it might be bigger than the input, or smaller).

In my initial pass, I used malloc() to create room for the first byte/bytes and then subsequently realloc() whenever I needed to append another byte/handful of bytes…it works, but it feels like this approach will needlessly hammer away at the OS and likely result in shifting bytes around in memory over and over.

So I made a second pass, which determines how long the result_string will be after an individual unit of the transformation (illustrated above with the FOO and BAR cases) and picks a ‘preferred allocation size’, e.g. 256 bytes. For example, if result_extent is 250 bytes and I’m in the FOO case, I know I need to grow the memory 12 bytes (newline, tab and 10 bytes from the input string) — rather than reallocating 260 bytes of memory, I’d reach for 512 bytes, hedging my bet that I’m likely going to continue to add more data (and thus I can save myself a few calls into realloc).

On to my question: is this latter thinking sound or is it premature optimization that the compiler/OS is probably already taking care of for me? Other than not wasting memory space, is there an advantage to reallocating memory by a couple bytes, as needed?

I have some rough ideas of what I might expect during a single conversion instance, e.g. a worse case scenario might be a 2MB input string with a couple hundred bytes of markup that will result in 50-100 bytes of data to be added to the result string, per markup instance (so, say 200 reallocs stretching the string by 50-100 bytes with another 100 reallocations caused by simply copying data from the input string into the result string, aside from the markup).

Any thoughts on the subject would be appreciated. thanks

  • 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-17T00:52:53+00:00Added an answer on June 17, 2026 at 12:52 am

    As you might know, realloc can move your data at each call. This results in an additional copy. In cases like this, I think it is much better to allocate a large buffer that will most probably be sufficient for the operation (an upper bound). In the end, you can allocate the exact amount for the result and do a final copy/free. This is better and is not premature optimization at all. IMO using realloc might be considered premature optimization in this case.

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

Sidebar

Related Questions

I'm working on a project in which I need to get some data from
I am working on a windows form project and having some problem with UserControl
I'm working on project which sends mails to multiple users using windows application,But what
I have a WiX project that I am working on which includes a Windows
I'm working on a game project in Java in which I need to be
I am working on a c# project which needs to work on Windows and
Actually i'm working in a project which need print a test page to a
I'm working on a windows phone project where I have some xml files with
I'm currently doing a project in C# working with windows forms. During the course
I'm working on a PHP project running on Windows and it would be so

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.