1

I want to avoid global variables when using my own classes in Arduino. Here is a example.

void setup(){
/* setup here */
  classA objectA;
}

void loop(){
  objectA.someMethod();
}

I know that my compiler will complain about it (out of scope, etc) but is it possible to do that or just forget it and use globally?

Thanks in advance!

  • Use globals. They're the recommended way. There are other ways, but global static declaration is the right way of doing it. – Majenko Sep 7 '16 at 22:39
  • @Majenko even if my classes getting bigger? I don't have to worry about that? – marquesm91 Sep 7 '16 at 22:40
  • 1
    Even if your classes getting bigger. By declaring it as a global with static declaration (MyClass foo;) you are reserving the RAM at compilation time. All other ways of doing it involve dynamic RAM allocation and that is bad on a microcontroller. – Majenko Sep 7 '16 at 22:42
  • @majenko Are you familiar with any patterns to help organize larger, non-trivial programs? Polluting the global space is frowned upon in most other problem domains. – Andrew Allbright Sep 8 '16 at 1:44
  • A program small enough to fit in the Arduino Uno is probably too small for global name space pollution to be a very serious concern. By using objects, you are already in a sense organizing the name space. If you do worry about your global name space, you could put all your globals in their own name space, or inside a big “app” object. – Edgar Bonet Sep 8 '16 at 8:07
1

If you want a variable to be available within more than one function it has to be global. There is no other possible way of doing it.

If you are only using the variable in the loop() function and nowhere else you can declare it static in loop(), which has the same effect of making it global, but it is only possible to see it within loop().

You can make a pointer global, then create a new object in setup() (using new...) and assign it to the pointer, but that has the potential of making a mess of your heap and should be avoided unless absolutely necessary. Also it's pointless, because you don't gain anything over having the object statically created in the global scope.

  • Seems enough for me. You convinced me! :) – marquesm91 Sep 7 '16 at 22:42
  • I read to avoid new and delete when coding for arduino. It is not my priority. I am just curious if is possible to do what I asked. – marquesm91 Sep 7 '16 at 22:45
  • You still end up with a global variable, just a different type of variable. – Majenko Sep 7 '16 at 22:46
  • This answer isn't satisfying. There has to be a way to balance programmer efficiency (e.g. organize logical bits of code in classes) with memory constraints. Is there not a compiler that can optimize the end-product in the global namespace way you suggest, but also provide the developer-friendly code organization that @marquesm91 is asking for? – Andrew Allbright Sep 8 '16 at 1:46
  • 1
    You could completely ignore the global space and the setup() function and just treat loop() like main(). Stick a while(1) at the end of loop() to stop it looping, define all your variables as local to loop(), then pass them all as parameters to where they are going. What do you end up with? You end up with global now inside loop() and allocated on the stack instead of the BSS. Big deal. Just stick with global, it's easier. – Majenko Sep 8 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.