Create C library: pointer style initialization or hidden state variables?

I am developing a library. Initialization basically returns a pointer to a malloced struct that contains all the internal states of lib. Although the user may not have enough uber-userful to create multiple instances of lib, this can be done if necessary. The syntax looks like this:

myLib* lib = lib_create();
lib_function1(lib, parameter1, parameter2);

Many C libraries work differently:

lib_initialize();
lib_function1(parameter1, parameter2);

Seems less flexible for the user, but also simpler and possibly faster (not sure about this?)

  • Which style do you prefer and which do you recommend?
  • Are there any performance issues with the pointer style? (I need better performance, as this is a real-time processing library, each time accessing a lot of variable internal states)
+4
2

, . , , myLib xy_create() xy_destroy(). , - , .

, " " . , , , , .

, create malloc(), , , . , , , , . , .

, . , , , , . , , .

+2

, . , , . , , .

, " " (mylib *lib ). , lib , .

, , . , .

+3

Source: https://habr.com/ru/post/1678935/


All Articles