0

Here is an example code, struct A is declared and then it's instance "instanceOfA" is defined inside main, so it become local variable and to access it in other functions I should pass it as an argument. On the other hand struct B is declared and it's instance defined as global variable that allows me to use "instanceOfB" anywhere I need it, directly instead of passing it around as an argument. The only thing I should worry about now is that some function may overwrite "instanceOfA" identifier right? since it's variables (a,b,c) already in it's own "namespace". So is it a bad practice to define struct object as global variable? What would you do? Sorry for my poor English.

struct A {
    int a,b,c;
};
struct B {
    int a,b,c;
} instanceOfB;

int main(void) {

    struct A instanceOfA;
    instanceOfA.a = 5;

    instanceOfB.a = 10;

}
ChLys
  • 13
  • 5
  • 1
    C does not have namespaces. C++ do – Basile Starynkevitch Feb 06 '15 at 17:13
  • Right. I meant just global and local variables. I'll edit that. Thanks. – ChLys Feb 06 '15 at 17:15
  • 1
    Side note: that `define` in the title is inappropriate; use `declare` or `instantiate` instead. – barak manos Feb 06 '15 at 17:17
  • 4
    As a general guideline, avoid objects in the global space as much as possible. – R Sahu Feb 06 '15 at 17:18
  • 1
    Every language has namespaces. Most just have very few, and don't offer much control over them. – Lee Daniel Crocker Feb 06 '15 at 17:18
  • @barakmanos: Why exactly is "*define*" inappropriate here? Moreover I'd say "*declare*" would be inappropriate. – alk Feb 06 '15 at 17:23
  • @alk: Well, `define` goes for definitions (like macros, types, etc). The question refers to the actual instantiation of the structure (not to its definition, which obviously goes into "global namespace", and has no meaning beyond compilation). – barak manos Feb 06 '15 at 17:25
  • An instantiated variable, had to be defined, hasn't it? At least in C this is common terminology. @barakmanos – alk Feb 06 '15 at 17:26
  • @alk: In short, the first 3 lines contain the definition of the structure. The second 3 lines contain a definition and an instantiation (but you could easily instantiate the structure separately from its definition). OP is obviously referring to the instantiation of the structure (the term "global" is meaningless with regards to the definition of the structure). – barak manos Feb 06 '15 at 17:29
  • @barakmanos: I'd say `instantiation` and `definition` of a variable (the OP is asking about a variable) are the same.... in C. Both are facts, the variable exists afterwards A declaration however is a promise. – alk Feb 06 '15 at 17:32

2 Answers2

1

I'm not sure what is the question about exactly, but here are some thoughts.

instanceOfB is a global variable, right? So, it is an evil that should be avoided if not strictly necessary.

On the contrary, struct A instanceOfA; inside function body looks fine for me, though we usually move types from struct namespace to global namespace by using typedef struct idiom to reduce typing.

Community
  • 1
  • 1
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • Thanks for useful link. Question was about "Structs as global variables: bad or good?" and I know that "globals" are generally considered as bad thing. I just hoped that maybe there were some exception for structs. I was just reviewing my old code where there was lots of passing structs around as an arguments and couple of those struct were needed almost everywhere so I thought maybe I should make them global. – ChLys Feb 06 '15 at 17:42
1

Yes, indeed, it is very bad practice to define anything as a global variable, not just a struct.

Yes, you will need to be passing it as a parameter. That's actually good, not bad.

It means that every single individual piece of code (function) in your program will only be able to modify what it is being given to modify, and not whatever it pleases.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142