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

  • Home
  • SEARCH
  • 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 6474867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:38:17+00:00 2026-05-25T06:38:17+00:00

For a small to-be-embedded application, I wrote a few functions + struct that work

  • 0

For a small to-be-embedded application, I wrote a few functions + struct that work as String Buffer (similar to std::stringstream in C++).

While the code as such works fine, There are a few not-so-minor problems:

  • I never before wrote functions in C that manually allocate and use growing memory, thus I’m afraid there are still some quirks that yet need to be adressed
  • It seems the code allocates far more memory than it actually needs, which is VERY BAD
  • Due to warnings reported by valgrind I have switched from malloc to calloc in one place in the code, which sucessfully removed the warning, but I’m not entirely sure if i’m actually using it correctly

Example of what I mean that it allocates more than it really needs (using a 56k file):

==23668== HEAP SUMMARY:
==23668==     in use at exit: 0 bytes in 0 blocks
==23668==   total heap usage: 49,998 allocs, 49,998 frees, 1,249,875,362 bytes allocated

… It just doesn’t look right …

The code in question is here (too large to copy it in a <code> field on SO): http://codepad.org/LQzphUzd

Help is needed, and I’m grateful for any advice!

  • 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-05-25T06:38:17+00:00Added an answer on May 25, 2026 at 6:38 am

    The way you are growing your buffer is rather inefficient. For each little piece of string, you realloc() memory, which can mean new memory is allocated and the contents of the “old” memory are copied over. That is slow and fragments your heap.

    Better is to grow in fixed amounts, or in fixed percentages, i.e. make the new size 1.5 or 2 times the size of the old size. That also wastes some memory, but will keep the heap more usable and not so many copies are made.

    This means you’ll have to keep track of two values: capacity (number of bytes allocated) and length (actual length of the string). But that should not be too hard.

    I would introduce a function “FstrBuf_Grow” which takes care of all of that. You just call it with the amount of memory you want to add, and FstrBuf_Grow will take care that the capacity matches the requirements by reallocing when necessary and at least as much as necessary.

    ...
    
    void FstrBuf_Grow(FstringBuf *buf, size_t more)
    {
        while (buf->length + more) > buf->capacity
            buf->capacity = 3 * buf->capacity / 2;
        buf->data = realloc(buf->data, buf->capacity + 1);
    }            
    

    That multiplies capacity by 1.5 until data is large enough. You can choose different strategies, depending on your needs.

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

Sidebar

Related Questions

I'm creating a small application on an embedded device that has a boa web
I read somewhere once that the modulus operator is inefficient on small embedded devices
I've only written a small amount of JavaScript that runs embedded in a Java
I am creating a small modal form that is used in Winforms application. It
I have an single threaded, embedded application that allocates and deallocates lots and lots
Our website lets users create small widgets that can be embedded on other third
Is it possible to have a embedded database in a small web application? What
I'm writing a small demo application in Java using Spring, that needs to have
We have a small embedded system without any video or serial ports (i.e. we
On a small embedded system project we have some code which we would like

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.