I have two functions. Which one is better and why? I want to run the program in embedded system.
1.
int Flag = 1;
void main(void) {
/* main body goes here */
}
2.
int Flag;
void main(void) {
Flag = 1;
/* main body goes here */
}
I have two functions. Which one is better and why? I want to run the program in embedded system.
1.
int Flag = 1;
void main(void) {
/* main body goes here */
}
2.
int Flag;
void main(void) {
Flag = 1;
/* main body goes here */
}
First you should not use a global at all (see A Pox on Globals).
If you insist however, you should prefer the first to ensure that the variable has a valid value for its entire lifetime regardless of how the code is maintained and where and when it is first accessed. The second introduces a maintenance issue where changes of data name or type and addition or removal of variables require changes in two places unnecessarily.
Another issue is that while the variable has an initial value of zero by the language definition, some embedded run-times optionally "optimise" start-up by not initialising global and static data to zero.
The first has less code to execute at runtime, but the cost is one assigment to one global variable which won't be measurable.
In this case, assuming that main()
is the entry point for your embedded system, then Flag
only needs to be set once, it seems, and that is best done with the initialization.
If, however, you were dealing with another function, call it somefunc()
, which is called (directly or indirectly) from main()
), and you need Flag
to be 1
on each entry tosomefunc()
before it calls other functions which look at the value in Flag
, then you need to assign to Flag
at the top of somefunc()
.
I assume your embedded environment explicitly supports void main(void)
— the standard expects the return type of main()
to be int
unless the implementation supports other types (and embedded systems might well be different about that).
int Flag = 1;
void main(void) {
/* main body goes here */
}
Here the memory is allocated in the data segment and that memory exists until the end of the program so this is unneccessary if this is Flag
is requried only in the main()
Then you can do
void main(void) {
int Flag = 1;
/* main body goes here */
}
Now the variable Flag
is allocated memory on the stack and used within main()
and the scope of the varible is just within main()
Now your question:
Which one is better and why?
As said in my comment both code snippets shown in your code doesn't have any impact on the memory utilization and as already said the first one has less instruction at execution time as there is no assignment statement int Flag =1
inside main()
and the second one has this assignemnt and there will be extra execution for this statement.
For this specific case use int Flag = 1;
It does not make sense at all when you initialize the variables 1 by 1, because the code to initialize it ends up taking up just as much if not more .data to accomplish the task.
int lookup_table[8192];
makes sense when you are generating a lookup table from a loop and want to minimize the binary size (if you have limited flash), since nothing gets added to your binary. A good example of this would be a frequently used 8k lookup table involving slow functions (eg. floating point trig functions) that can be loop-initialized once at startup so that the 8k*sizoeof(int) table doesn't get stored in the binary. (See various mp3 decoder implementations for examples)
Note: the loop initialization of the lookup table will still take as much memory to store as well as requiring additional startup time the first time it is used (and therefore initialized), but will reduce binary size and could actually reduce startup times if that specific table is large and uncommonly used.
int lookup_table[8192] = { ... };
makes sense if you are on an embedded system with a generous amount of XIP (execute-in-place) capable flash. Pre-generating will not only eliminate the additional startup time, but since it is XIP, no RAM will be used, but at the expense of a larger binary.