Data

Limiting Scope

An important factor involving modular software is scope.
The scope of a variable defines which software modules can access the data.
Minimizing scope reduces complexity, and simplify testing.

Variables with a restricted access are classified private, 
and variables shared between multiples modules are public.

In general, when we limit the scope of our variables, a system is easier to design (because the modules
are smaller and simplier), easier to change ( because code can be reused), and easier to verify (because
interaction between modules are wee-defined).

Allocation   
RAM, ROM, Stack or Register ?

We can specify where the data is allocated. If it is a variable that needs to exist permanently,
we will place it in RAM as a global variable. If it is a constant that needs to exist permanently,we will 
place it in ROM using const. If the data is needed temporarly, we can define it as local. The compiler will 
allocate locals in registers or on the stack in whichever way is more efficient.
 
/***********************************************************************/ 

// In C, we define global variables outside of the function

int32_t PublicGlobal;           // accessible by any module 
static int32_t PrivateGlobal;   // accessible in this file only 
const int32_t Constant = 12345;  // in ROM 

void function(void) {      
static int32_t veryPrivateGlobal ;  // accessible by this function only      
int32_t privateLocal;                // accessible by this function only 
  
} /************************************************************************/ 

Global variable is a variable with permanent allocation in RAM. 
(PublicGlobal,PrivateGlobaland veryPrivateGlobal are global variables). 
Constant will be defined in ROM, and cannot be changed. 
Local variable is a variable with temporary allocation. ( privateLocal may exist on stack or in a register). 

The second aspect of the data is its scope. Scope specifies which software can access the data. 
Private variables have restricted scope, which can be limited to the one file, the one function, or even 
to one {} program block. In general we wish minimize the scope of our data. Minimizing scope reduces 
complexity and simplifies testing. If we specify the global with static, then it will be private and can 
only be accessed by programs in this file. If we do not specify the global with static then it will be public, 
and can be accessed any program. By example, the PublicGlobal variable can be defined in other modules using 
extern and the linker will resolve the reference. However, the PrivateGlobal cannot be accessed from software 
in other file. We put all the globals together before any function definitions to symbolize the fact that 
any function in this file has access to these globals. If we have a permanent variable that is only access 
by one function, then it should be defined inside the function with static. For example, the variable 
veryPrivateGlobal is permanently allocated in RAM, but can only be accessed by the function.


static  modifier

In C, a static local has permanent allocation, which means it maintains its value from one call to the
next. It is still local in scope, meaning it is only accessible from within the function.
It is now permanent, but still private.

In C, we create a private global variable using the static modifier. Modifying a global variable with
static reduce its scope. A static global can only be accessed by functions within the same file. Static
globals are private to that particular file. Functions can be static also, meaning they can be called only
from other functions in the file.

Const modifier 

In C, a const global is read only, cannot be changed. It is allocated in the ROM. Constants, of course,
must be initialized at compile time.


Leave a comment