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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:53+00:00 2026-05-26T11:35:53+00:00

I’m not really into C design patterns, so my doubt is potentially simple (although

  • 0

I’m not really into C design patterns, so my doubt is potentially simple (although a little specific).
The real application of this question is harder to explain, so let me simplify it.

Suppose I have an array, in which I want to store prime numbers. The number of primes this array contains is defined by NUMBER_OF_PRIMES, a constant defined at compile time.

Thus, we have:

unsigned primes[NUMBER_OF_PRIMES];

If the size was fixed, I could pre-compute the primes and initialize the array as usual:

unsigned primes[NUMBER_OF_PRIMES] = { 2, 3, 5, 7, ... };

But that would be rather ugly if NUMBER_OF_PRIMES were, let’s say, greater than 200. I want some way to run a function at runtime, but before main(), that will put those primes numbers there. Of course I could do:

unsigned* primes = make_primes(NUMBER_OF_PRIMES);

which would allocate the necessary memory and run before main. The main problem is: this array would be in a header file, but it’s value would contain something that’s hidden inside the corresponding .c file. what I thought I could do is:

/* Header file */
unsigned primes[NUMBER_OF_PRIMES];

/* C file */
int nothing = initialize_primes(); /* This function would write the 
values to the array, using things that are not available in the
header (it is in the .c, so it can reference them), and return anything */

Another way is obviously putting initialize_primes() in the header file, and asking the user to call it inside the main function (like some libraries’ init() function). However, I’d like not to force main() to contain a call to this function.

My question is if there is any elegant way to do this that is universally accepted/used, or if this is ridiculous and I should ask main() to call an init() function to avoid unnecessary, obscure code.

As I said, I’m not working with anything related to primes; this is just an example with the same challenge.

Best regards.

  • 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-26T11:35:54+00:00Added an answer on May 26, 2026 at 11:35 am

    One trick that you can do is this:

    // primes.c
    static unsigned *p = NULL;
    
    const unsigned *primes(void)
    {
        if(p == NULL)
          {
            p = malloc((PRIMES_COUNT + 1) * sizeof *p);
            *p++ = 0; // if user accidentally frees p now, it will segfault
            // populate p
            atexit(primes_cleanup);
          }
        return p;
    }
    
    void primes_cleanup(void)
    {
        if(p)
          {
            free(p - 1); // correct way to free the pointer
            p = NULL;    // now calling _cleanup twice is fine
          }
    }
    

    The user uses primes()[N] to get the desired element, and it’s guaranteed to be initialized. Even if you store the pointer and use it later, it’s guaranteed to work (unless someone cast away the const and changed your list when you weren’t looking). If you still want variable-like syntax, use this:

    // primes.h
    const unsigned *primes(void);
    void primes_cleanup(void);
    #define PRIMES (primes())
    

    To the end-user, PRIMES will look like any other variable. Win.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.