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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:36:54+00:00 2026-06-09T11:36:54+00:00

If this is a beginner’s question, my apologies – most of my programming has

  • 0

If this is a beginner’s question, my apologies – most of my programming has been in very high level langauges, and I have limited expertise in C. (This is the sort of thing I could do very easily in languages such as Matlab, Octave, Sage, Maxima etc, but for this I need the speed of C).

But anyway… I have an array whose size is set at run time with malloc:

int *A = malloc(m * sizeof(int));

where m is computed from some values provided by the user. I have a function “update” which updates the array (or, if you prefer, takes the array as input and returns another as output). This update function may be called upwards of 10^8 times.

So the function itself can’t introduce the appropriately sized output array with malloc, or the memory will be used up. So, for example, I can’t do this:

int * update(int *L) /* produces next iteration of L */
{
  int *out = malloc(m * sizeof(int));
  /* do some stuff with L and produce new array out */
  return (out);
}

I’ve tried to make out a static variable outside the update function:

static int *out;

and define its size in main:

out = malloc(m * sizeof(int));

But this doesn’t seem to work either.

Anyway, I would be very grateful of some advice – I think I’ve exhausted the excellence of google.

  • 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-09T11:36:55+00:00Added an answer on June 9, 2026 at 11:36 am

    Allocate the array outside of update, then pass a pointer to it:

    void update(int const *L, int *out)
    {
        // whatever
    }
    

    Call as

    int *A = malloc(m * sizeof(int));
    if (A == NULL)
        // handle error
    
    for (i=0; i < N_ITER; i++)
         update(L, A);
    

    Though you may want to redesign the program so that it updates L in-place.

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

Sidebar

Related Questions

This is a beginner level question for asp.net MVC I have the following code
This is a beginner-level question. I have a catalog of mtypes: mtype_id name 1
i know this question was asked before but i have this very weird beginner
I am very new to Javascript and Jquery, so my apologies for this beginner's
Hi this is ROR beginner's question. I have creat controller.rb and view hello.rhtml following
Ok, this is a beginner's question. I have a main page with a header
I know this is a beginner's question, but I've been searching online and all
I'm a beginner with this so sorry for the silly question, I have a
Ok I have another question and I'm a beginner at this. I have this
This is a beginner question, I have to create some SQL Reports with business

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.