Question

I want to use GMainLoop. I wonder if it is better to use global or local variables. I want to call a function every 5 secs. It uses some heap variables and I want to malloc only once. I have two ideas. I can put all arguments in a struct. But sometimes it would be struct in struct in struct. So the name nomenclature will be complex and bad readable. I can also make all data gloal. It would be something like:

typedef struct data_t
{
  unsigned char *pUncompressStreamData;
  unsigned char *pStartUncompressStreamData
  analyzers_t analyzers;
  GTree *tree;
}data_t;
main_loop(data_t data )
{
   //data_mallocs and other stuff
} 
int main()
{
    data_t data;

    g_timeout_add_seconds ( 5, main_loop, data);
    /free all data member
} 

OR

unsigned char *pUncompressStreamData;
unsigned char *pStartUncompressStreamData
analyzers_t analyzers;
GTree *tree;

main_loop(data_t data )
{
   //data_mallocs and other stuff
} 
int main()
{

    g_timeout_add_seconds ( 5, main_loop, NULL);
    /free all data member
} 

Please give me clues.

Was it helpful?

Solution

Use local variables or structures (more readable and more modular). Notice that you could pass them, e.g. as the address of some struct, as the client data of g_timeout_add_seconds. So prefer the first version to the second. However, you main should initialize data e.g. with calloc then free it after the Glib event loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top