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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:26+00:00 2026-05-25T14:19:26+00:00

I have a function which is called many times in my project: void foo(int

  • 0

I have a function which is called many times in my project:

void foo(int bar)
{  
    char arr[1024];
    //...do some operation on arr according to value of bar  
}

Now, in some rare cases, I am getting a segmentation fault when the value of bar is large. I need to increase the size of arr, whose size I can know from value of bar.

The obvious solution seems to dynamically allocate memory for arr depending on size of bar. However, this function is called very frequently, and I think allocating memory each time can decrease performance.

What should be my strategy to tackle this?

  • 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-25T14:19:26+00:00Added an answer on May 25, 2026 at 2:19 pm

    Two suggestions:

    1. Allocate dynamically, but reuse the buffer. So foo will also get a buffer as a parameter from the calling function (if called from the same function in a loop, for example. I wouldn’t pass the buffer all over the program if foo is called from 100 different places). This way, you only have to allocate once the buffer needs to be increased.
    2. Declare a large local array as you do now, but make sure it’s large enough before using it. If not, dynamically allocate one on the heap. Assuming the local array is large enough for most cases, you’ll only allocate on the heap on rare occasions.

    Edit:

    Regarding #2, @David Heffernan got heat for suggesting the same option, as it might be a complication of the code. I don’t think the following is very complicated:

    void foo(int bar)
    {
        char localArr[1024];
        char* arr = localArr;
        if (sizeNeeded > 1024)
            arr = malloc(sizeNeeded);
    
        // ... use arr in logic ...
    
        if (arr != localArr)
            free(arr);
    }
    

    I’ve used similar code in some frequently invoked callback where I couldn’t reuse the buffer, and eliminating the malloc on most cases has definitely improved performance. Whether that’s the best solution for the OP I really don’t know.

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

Sidebar

Related Questions

here i have one function which is call many times in one application this
I have a function which calculates distance from database records. This function is called
I have a function, which is called sometimes with regular, sometimes dynamic arrays. If
I have a Function in CFC file, which will be called from .cfm file
I have this code which is called at an onChange event of an function
Now I'm sure this is super easy, I have a function called remove_deposit which
I have a class called User with static function loginRequired(), which returns false if
I have 5 functions which gets called 10000+ times (on an average). All of
I have a dll which includes a function called ReadPort that reads data from
In an actionscript function (method) I have access to arguments.caller which returns a Function

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.