简体   繁体   中英

c accessing data members in different translation unit

I wrote some code that uses function pointers to link modules functions in c. I was wondering is it possible to use those function pointers to access state in another translation unit.

I wrote this struct to handle life cycle functions so that I can have a module doing everything, you pass in a pointer to this struct with your functions and they get called when they are suppose to.

typedef struct {
    void (*init_func_ptr)(void);
    void (*fixed_update_func_ptr)(void);
    void (*variable_update_func_ptr)(double alpha);
    void (*variable_render_func_ptr)(double alpha);
    void (*pause_func_ptr)(void);
    void (*resume_func_ptr)(void);
    void (*resize_func_ptr)(int width, int height);
    void (*handle_event_func_ptr)(key_event *e);
    void (*quit_func_ptr)(void);
} lifecycle_ptrs;

i typically set it up like this

    lifecycle_ptrs* ptrs;

    ptrs = calloc(1, sizeof(lifecycle_ptrs));
    ptrs->init_func_ptr = &init;
    ptrs->fixed_update_func_ptr = &fixed_update;
    ptrs->variable_update_func_ptr = &variable_update;
    ptrs->variable_render_func_ptr = &variable_render;
    ptrs->pause_func_ptr = &pause;
    ptrs->resume_func_ptr = &resume;
    ptrs->resize_func_ptr = &resize;
    ptrs->handle_event_func_ptr = &handle_events;
    ptrs->quit_func_ptr = &quit;

this ptrs struct is setup in the module that defines main and is passed as a function argument. Once this is passed the program goes into an infinite loop until the user requests to quit.

So far this works pretty well and does what I want to do.

What I was curious about now is, is it possible to have something like this where I am able to access variables from the other module?

Let's say module a looks like this

modulea.c
int a;
int b;
char t[] = "test";

moudleb.c
struct t;
float 12.0;

is it possible to access module a's member variables from moduleb or visa versa?

I could write functions to get each thing but I feel there might be more elegant and maintainable way to do that. I could also define a struct and do a swap function where module a get access to the variables in module b and visa versa but again this seems a bit odd to me.

Are there any other ways to do this? I am more concerned about maintainability but any insights would help because right now I am at a loss.

Yes, technically it is possible, simply use extern .

However, there is no reason why you should ever do that . If you ever come up with a need to access (non-constant) variables in another file, then that means that your program design is broken.

And if you go through with extern , it will become even more broken, you'll get complete spaghetti code with very tight coupling, where everything in the program depends on everything else and all bugs will fail-escalate through the program and tear down things completely unrelated to the bug.

So the proper solution is to step back and think twice about the overall program design.

Instead of such spaghetti-coding, use object-oriented design and write each h and c file pair so that they together form an autonomous module (call it class, ADT or what you will), which is only concerned with it's own designated purpose. All file scope variables inside such ac file should be declared static to block access from outside. Access to such variables, if at all needed, should be done through setter/getter functions. The only kind of variables that is acceptable to share directly across files are read-only constants.

Though note that you should avoid file scope variables as far as possible, as they will make the code non-reentrant.

Easiest way to share members between different modules is to use headers files.

You could add a modulea.h file to your project like:

#ifndef modulea_H_
#define modulea_H_

extern int a;
extern int b;
extern char t[];

#endif

Then you can #include "modulea.h" in your moduleb.c source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM