Dear all,
I have no training in C++ and like, and I my knowledge comes from few books and a lot of studying other people codes.
It seems that every time one creates a structure for a variable, that structure is defined before the setup instruction.
However the place where the variable using that structure is created seems to depend on the programmer.
I have seen it defined in the setup section and in the loop section.
My question is: what are the benefits (if any) of placing this command in either place.
Do I need to place in the loop so the values are updated?
Thanks
It all depends on the desired scope of the variable or structure (struct).
A variable declared "before setup()" or outside of any function is visible to the whole program. Global scope.
A variable declared inside setup() or any function is only visible to that function. Local scope.
Are you referring to a struct or a simple variable like byte myVariable
Google local/global variable.
For simple microcontroller code you can safely declare all variables at the top before setup. As you learn more you will want to limit the scope of variables by declaring them inside the functions they are used in.
I believe this was how the Fiat Chrysler vehicles Bluetooth hack worked. A global variable wrote to functions they shouldn't have and allowed the Bluetooth connection to control other stuff like engine, brakes, and steering.
I'm puzzled by the use of the definite article in that phrase, like it's a well-known thing.
Another benefit is that local variables "come to life" when control enters the function where they are defined, and then "die" when control leaves that function. Global variables are eating up data space all the time, perhaps even when they are not really needed.
Having both local and global variables with the same name is a common new user mistake. We would have caught that if Fiat Chrysler had run their code past us.
(removing tongue from cheek).
Well fortunately the likelihood of people visiting this post also having access to a search engine is pretty high. And I never said it was well known. It's the most well known hack of Bluetooth on Chrysler vehicles though.
Then there is the static keyword that effects the "lifetime" of a local variable. A static local variable does not die when the function is done.
Chrysler's not that big a deal in this part of the world.
I thought maybe this was something new, but it looks like pretty old news.
groundFungus; that's true. A static variable defined within a function has its scope restricted to that function, but it retains its value between calls to the function. A static variable defined outside a function has scope from its point of definition to the end of the file in which it is defined. This can be very useful when a project is comprised of multiple source code files.
Thank you very much for the replies.
I think I understand it better now, although my primary concern about assigning a struct to a variable in the loop section was "waste of computing power".