Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda