Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to use global variables in c

I have read the writing on the wall, Avoid Globals. Which leads to the obvious question, how best to do it?

I obviously want to do it with a current project. The goal is to let a distant PC send "keystrokes" to applications that already have an stdin reader. The idea is to let the existing code detect there is no pending keystroke, then check if there is a "udp keystroke" and, if so, stuff it in so it appears to be keyboard input. Minimally invasive and won't require gobs of retro-fitting in other people's code.

So I cobbled up a small UDP socket reader that uses a setup() function to open and bind the port, then a service() function in a loop that uses a nonblocking select() once, no loop, just check if there is anything to read right now. If so, read the data from the socket and do something with it, else return a 0.

// pseudo c
char c;

setup();
while (1)
{
   c = check_for_keyboard_entry();
   if ( c == 0 )
      c = service();
   handle_keypress( c );
   do_a_bunch_of_other_stuff();
}

The obvious way to do this is with a few globals to transfer the port, timeout, sockaddr's etc. between the two functions. But, thou shalt avid globals, right?

So what is a preferred way to transfer the six or eight vars between the functions?

If I were to use static vars in the setup(), would they be accessible by the service() routine?

I suppose a struct that gets malloc-ed and passed around would work. I'd ought to have a cleanup() to close the socket and free the memory.

REMEMBER, THIS IS A C QUESTION. NO C++!

like image 902
Wes Miller Avatar asked Jul 08 '13 21:07

Wes Miller


People also ask

How do you avoid global variables?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.

Should you avoid global variables in C?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

What can I use instead of global variables in C?

Use Local Variables Instead The idea is that the global variable gState is replaced by the local variable sState in the script of your main application stack.

How can parameters be used to avoid the use of global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.


1 Answers

You can just pack all that information into a struct. The struct can be visible to the client or an opaque type. An opaque type may be a better choice so you can hide the data (encapsulation) for complex types.

typedef struct {
 port_t port;
 timeout_t timeout;
 sockaddr_t sockaddr;
 etc_t etc;
} my_stuff;

then pass it around by reference:

void Foo(my_stuff*);

I suppose a struct that gets malloc-ed and passed around would work. I'd ought to have a cleanup() to close the socket and free the memory.

Generally, a struct would not always need to be malloc'ed. But yes, this may be a case where it is required.

like image 60
justin Avatar answered Sep 30 '22 11:09

justin