Alex Rivera | Logout

Is it possible to avoid global variables in a strictly procedural program?

Asked 2010-04-29T15:48:28.650
12

Being a developer born and raised on OO, I was curious to hear how it's possible to avoid global state in a procedural program.

c
Edit
Report

2 Answers

4

A global variable is nothing but an implicit procedure argument. Make it explicit and the global variable goes away.

Note: the fact that you no longer use a global variable does not mean that you no longer use global state! What we did above was just a purely syntactical transformation, the semantics of the program haven't changed at all. It's just as non-composable, non-modular, non-threadsafe, non-parallelizable as it was before.

answered 2010-04-29T19:03:54.257
0

You can have variables on stack or in heap that will exist during all the program life.

Passing object style structure pointers to every function is a good way to have OO C coding style.

(I would suggest to have a look in linux sources)

answered 2010-04-29T15:56:09.193

Your Answer