开发者

How to avoid globals in C?

开发者 https://www.devze.com 2023-01-10 07:39 出处:网络
As a beginner, I read everywhere to avoid excess use of global variables. Well how to do s开发者_运维知识库o? My low skill fails. I am ending up passing tons of structures and it is harder to read tha

As a beginner, I read everywhere to avoid excess use of global variables. Well how to do s开发者_运维知识库o? My low skill fails. I am ending up passing tons of structures and it is harder to read than using globals. An tips on going through this problem/application structure design?


Depending on what your variables are doing, global scope might be the best scope. (Think flags to signal that an interrupt has arrived, and should be handled at a convenient time in the middle of a compute loop.)

Small utility programs can often feel much cleaner by using global variables (I'm thinking especially of small language parsers); but this makes it much harder to integrate the small utility programs into larger programs in the future. There are always trade-offs.

But chances are good the "correct" data organization will not feel quite so cumbersome. If you post code here, someone may be able to suggest cleaner layout, but the real problems come when code grows beyond easily-understood small samples.

I have a LOT of favorite programming style books, but I think the best I know of to address this situation is The Elements of Programming Style, by Kernighan and Plauger. It's quite old, and difficult to find, but short, sweet, and well worth finding used somewhere.

It's not as short, it's not as sweet, but still well worth finding Code Complete, 2nd edition. It's much more detailed, provides much more code, and provides much more diversity involved in designing software. It's excellent, but might be more intimidating.

There's nothing like studying the masters: the code in Advanced Programming in the Unix Environment, 2nd Edition is phenomenal, well worth every hour of study.

And, of course, there's always experience, but that takes time to acquire. Learning lessons from your own mistakes tends to stick much stronger than learning lessons from other people's mistakes. So keep at it. :)


I'd suggest Structured Design by Yourdon and Constantine. An old book by computer standards (it has examples involving tapes!) but very sound on the problems you are having.


Here are two options that you could use to improve your situation:

  1. For read-only structures, have functions that can control access to the data with a const pointer:

    struct my_struct;

    const my_struct* GetMyStruct(void) const;

  2. Limit the exposure of a global structure by declaring it static. This way it will only have file scope:

    static mystruct myStructInstance;


If your program it the sort of "small" project where global variables don't feel so bad, but you think you might need to integrate it into a larger project in the future, a very simple solution is to add a single context pointer argument to each function and store all your "global" variables in there. If you always name it the same thing, you can even do stuff like:

#define current_filename   context->current_filename
#define option_flags       context->option_flags

etc. and your code will look virtually identical to how it would have looked with globals, except that you'll be able to have multiple instances of it in a single program, integrate it into a library, and so on with minimal fuss. Just keep those defines in a private header used by your source modules, not the public interface header.


@PeterK Problem is that structure as itself is always presented in C books a as container that can be declared/passed many times to different functions and that is a thing that may confused me and I never thought to use it as a simple one instance global container (and that may make my code more readable).

I am writing 3 phase motor control application to control 1 motor. Based on what all you wrote please check if my current ideas of solving problem is right:

  1. Pack some global information in structure according to function ex. (sInverterState, sButtonsState, sInverterParameters etc.)
  2. If I write menu UI I can use static variables in C file and don’t care about passing structs when I have only 1 LCD. I don’t want to make it look like GTK++.
  3. Writing reetrant code is not for me yet and its overdoing for this purpose.
  4. Get proper education in IT field.

I may end up with lots of globals but at least they are nicely packed and readable.

0

精彩评论

暂无评论...
验证码 换一张
取 消