Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Best way to abstract away an init function?

I am making a low level library that requires initialization to work properly which I implemented with a init function. I am wondering if there is a way to make the init call be called once the user calls a library function ideally without:

  1. Any overhead
  2. No repeated calls
  3. No exposed global variables. (my current solution does this, which I don't quite like)

my current solution as per comment request:

bool isinit = 0;

void init()
{
  isinit = 1;
  // init code
}

void lib_function()
{
  if(!isinit) init();
  // function code
}

The compiler seems to be smart enough (using -0fast on gcc) to not make that comparison each time a lib_function is called, but this still exposes a global variable which I don't like.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you're happy with a solution that is a common extension rather than part of the C standard, you can mark your init function with the constructor attribute, which ensures it will be called automatically during program initialization (or during shared library load if you eventually end up using that).

over 4 years ago · Santiago Trujillo Report

0

Best way to abstract away an init function?

Surely your library has some state. Typically, a library exposes functions that work on a specific structure. Do not use global variables - do not write spaghetti code. Expose the structure that holds the state of your library, and make all functions of your library take a pointer to the structure as an argument. Use a namespace - prepend all exported symbols with a prefix. An init function is just like int lib_init(struct lib_the_struct *t); - it will be self-understandable that users need to initialize the structure with that function before use. For example: fopen(), pthread_create.


Write an init function in your library. Write clear documentation stating, that the user of your library has to call the function once before calling any other function. For example: https://curl.se/libcurl/c/curl_global_init.html .

over 4 years ago · Santiago Trujillo Report

0

I would fix this with assert so that the if will dissappear in release build and if you forget to call the init_function somewhere you get the error while developing.

Also turn isinit into a static so every library can have its own variable with the same name.

    #include <assert.h>
    
#ifndef NDEBUG
    static int isinit = 0;
#endif

    void lib_function()
    {
      assert(isinit && "library: init not called");
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!