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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:12:56+00:00 2026-06-01T00:12:56+00:00

Which is preferable way to allocate memory for a function that is frequently allocating

  • 0

Which is preferable way to allocate memory for a function that is frequently allocating and freeing memory ? Assume this function is getting called around 500 to 1000 times a second on a 1GHz Processor.

(Please ignore static and global variables/allocation. I am interested only this specific case:)

void Test()
{
    ptr=malloc(512)   // 512 bytes
    ...
    free(ptr) 
}

OR

void Test()
{
     struct MyStruct localvar; // 512 byte sized structure
     ... 
}
  • 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-01T00:12:57+00:00Added an answer on June 1, 2026 at 12:12 am

    stack allocation of local variables is faster than heap allocation with malloc. However, the total stack space is limited (e.g. to several megabytes). So you should limit yourself to “small” data on the local stack. (and 512 bytes is small by today’s standard, but 256Kb would be too large for local stack allocation).

    If your function is very deeply recursive, then perhaps even 512 bytes could be too big, because you’ll need that for each recursive call frame.

    But calling malloc a few thousands time per second should be painless (IMHO a typical small-sized malloctakes a few dozens of microseconds).

    For your curiosity, and outside of the C world, you might be interested by old A.Appel’s paper garbage collection can be faster than stack allocation (but perhaps cache performance considerations could weaken this claim today).

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

Sidebar

Related Questions

Which is preferable? Assume this code before each: byte[] data = Encoding.ASCII.GetBytes(Test Packet Message);
Which one is preferable or more clear? public int FrozenRegionWidth { get; set; }
In which cases storing data as XML is preferable to RDBMS and why? Can
Which way do you prefer to create your forms in MVC? <% Html.Form() {
I know this question will probably provoke more discussion than concrete answers (which I
In my application I have two queries which will be quite frequently used. The
I have code like this DateUtils.getRelativeDateTimeString(context, due, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS,0) which is outputting strings formatted
I'm looking for a way to implement a bidirectional RPC in node.js (means that
It's well known that using forward declarations is preferable to using #includes in header
How can I reserve and allocate shared memory without the backing of a file?

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.