Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda