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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:03:27+00:00 2026-05-30T12:03:27+00:00

So deep inside a library I need a function which allocates a string, writes

  • 0

So deep inside a library I need a function which allocates a string, writes a provided floating point number to the string with %f format, and returns it. snprintf() returns the number of characters that would be needed, which would allow me to allocate the right size with 2 snprintf() calls. Unfortunately this is a somewhat performance-critical piece, so I’d like to avoid that as *printf() can be slow (yes, in some benchmarks it does show up at #1 on the profile).

Another way is to use some of the C99 functions to figure it out, but libm calls take a fair bit of time as well. A (very much simplified, no error handling etc.) version of my function would look something like the following


// Return a string containing number x in %f format with d digits after the decimal point.
char* my_function(double x, int d)
{
    int n = ceil(log10(pow(2, ilogb(x))));
    // 3 extra chars, the initial sign, the ".", and the terminating null.
    char *s = malloc(n + d + 3);
    snprintf(s, n + d+ 3, "%#-+.*f", d, x);
    return s;
}

A potentially even faster way would be to allocate a string which is "large enough", and then do the second snprintf call only if the unlikely happened and it turned out to be too short.

It's also possible to structure the code so that I can use stack allocation with alloca() and/or C99 VLA's, but as stack space is often quite limited I'd like to avoid blowing a lot of memory on a worst-case sized buffer.

Any better idea?

  • 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-30T12:03:29+00:00Added an answer on May 30, 2026 at 12:03 pm

    Are you running in a memory constrained system? If you’re not at the point where you really need to count bytes, simply determine your worst case and make all allocations this size.

    Another option is to make the caller provide the buffer.

    Finally, do you know if you can state that there will not be a need for more than a finite number of these values to exist at once? If so, pre-allocate the space for them and don’t do the malloc() within your function. Since you say performance is critical, you should be trying to avoid malloc() as well.

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

Sidebar

Related Questions

Need a ref to the window in which a view is inside (no matter
I have a function buried deep inside an assembly, eventually this function will get
I need a python 3.1 deep update function for dictionaries (a function that will
The fopen function returns a pointer to a FILE structure, which should be considered
Deep inside my code, in a nested if inside a nested for inside a
//deep linking $.fn.ajaxAnim = function() { $(this).animW(); $(this).html('<div class=load-prog>loading...</div>'); } $(document).ready(function(){ contM = $('#main-content');
//deep linking $(document).ready(function(){ contM = $('#main-content'); contS = $('#second-content'); $(contM).hide(); $(contM).addClass('hidden'); $(contS).hide(); $(contS).addClass('hidden'); function
I have a custom control's library. Now there's a control which looks like a
Deep inside my WPF object hiearchy I create a Window object. However, I want
I have a nasty exception that seems to occure deep inside the DataGridView Code.

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.