Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C, design: removing global objects

I'm creating a small Avida-style life simulation. I started out with a very basic, everything-is-global 600-line program in a single file to test some ideas, and now I want to create a real design.

Among other things, I had a global configuration object that every other function got something out of. Now, I must localize the object and pass pointers around. Thing is, mostly everyone needs this object. I've thought of three possible solutions:

  • a) Keep the configuration object global (simplest, though not really a solution)

  • b) Store pointers everywhere they are needed (easy enough, though a waste of memory, since some small plain-old-data structures would need it).

  • c) Create factories for the POD types that need access to options, and have the factory perform all operations on them.

Of my ideas, only (c) sounds logical, but I don't want to needlessly complicate the structure. What would you guys do?

I'm fine with new ideas, and will provide whatever information about the program you want to know.

Thanks in advance!

like image 401
slezica Avatar asked Jul 09 '11 19:07

slezica


People also ask

How do you reduce global variables?

Global variables can increase memory requirements and reduce execution speed. To reduce global RAM for a nonreusable subsystem, you can generate a function interface that passes data through arguments instead of global variables. The Subsystem block parameter Function interface provides this option.

Are global objects allowed in C++?

It can be convenient to use C++ objects as global variables. You must be careful about what you do in the constructor, however.

How do you delete a global object in C++?

you can put those variables in aclass , then create pointer to object of that class in the 1st place will use those variables then pass this pointer to every methode need those variables , don`t forget to delete the object when you will not use .

What global& local variable?

A global variable is a variable that is accessible globally. A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.


1 Answers

I have to agree with @Carl Norum: there is nothing wrong with the global config setup you have now. You say that everybody "got something out of" it. As you know, the problem with globals comes when everybody writes into them. In your case, the config info truly is needed globally so deserves to be global.

If you want to make it be a little more decoupled and protected -- a little less global-ish -- then why not add some read/write access routines.

See, storing pointers everywhere isn't going to really solve the problem: it will only add a layer of indirection that will merely disguise or camouflage what are, in reality, the global accesses that are making you nervous. And that extra layer of indirection will add juuuuust enough room for juuuuust a teeny-weeny little bug to creep in.

So, bottom line: if stuff is naturally global then make it global and don't worry about the usual widespread received wisdom that's mostly correct but might not be the right thing in your application. To always be bound by the rules/propaganda that CS teachers put out there is, imo, the perfect example of a foolish consistency.

like image 153
Pete Wilson Avatar answered Oct 21 '22 14:10

Pete Wilson