Tuesday 5 July 2016

Memory Allocation in C for Variables

In this blog we will disused about the variables, initialization of variables, Local and Global Variables, Static Variables, RAM memory architecture and variables memory allocation in embedded C.

What are variables and constants?
Variables in C language are memory location identified by some name and program can manipulate this memory data. Each variable has some type of data type. Which define the type of data it can store and range of data. Variables are stored in the RAM memory location during execution time and used for saving critical data during run time.

Constants variables are name of memory location and usually stored in the program memory. They are temporarily stored in the RAM memory when they are examined. Constant value cannot be change during program execution.
For a specific compiler, range of these variables may vary from one system to another system.  In general range of data types are as follows:

Data Types
No Of Bits
Ranges
Unsigned Char
 8 bit
0 to 255
Signed Char
8 bit
-128 to +127
Unsigned int
16 bit
0 to 65535
Signed int
16 bit
-32768 to +32767
Unsigned long int
32 bit
  0  to 4294967295
Signed ling int
32 bit
-2147483648 to  +2147483647
Floating data
32 bit
-3.4E38 to +3.4E38

LOCAL Variables

Local variables are those whose scopes are limited up to a module. When we declare a variable in any function its scope will be up to this function. These variables are store in the stack memory. When exit from function occurs all variables losses there data.
For example following variable initialize local to function:

void function1(void)
{
unsigned char var1;
unsigned char var2;
unsigned int var3;
}

On function call these variables get memory allocation in the stack memory.
If we initialize data in function using dynamic memory allocation then it wills stores in the heap memory to save data. In heap memory if we not free the memory locations then it will not losses there data.

void function2(void)
{
unsigned chr *ptr1=malloc(10); // reserve 10 memory location
}

Global Variables
Global Variables are those which are declared globally. Scope f these variables are global. More than one function can access these variables. After reset controller copies these data to ram memory, during program execution these variables retain there values until power does not removed.
For example following variables are declared globally:
unsigned char var1,var2=0;
unsigned int varb3=0;
void main(void)
{
unsigned char var4, var5;

while(1)
{                  // software code
}
}

In the above example var1, var2, var3 are declared globally outside of main function and var4, var5 are declared local to main function. In following program var4 and var5 also work as global variable until there any type of reset. After entering in main function program remain in the while (1) loop.

Static variable
Static variables are those variables which are allocated a static memory location during initialization. All global variables are static in nature. It is also possible to declared static variable in a function. For example:

void function2(void)
{
          unsigned char var1;
          static unsigned char var2;      // static variable
}

In this example var2 is initialized as static variable. When function is called var2 allocated in the ram memory and retain there value after exit from function.  If you call these variable in another function then compiler will give error.

Memory Modal of Microcontroller
Memory model of microcontroller defines the memory allocation for Code memory, variable memory, stack memory, heap memory etc. When a program is loaded in microcontroller memory following data is stored in the memory:
·        Program to be executed
·        Constant variables
·        Initialized global static variables
·        Local Memory & Dynamic memory allocated data
When first time program is loaded in microcontroller all these data except local variables stores in the flash memory. In general Memory modal may be defined as shown in figure below:


Executable program, constants, read only data stores in the Flash memory’s Read Only section. After source code all the read write data stores in the flash memory.

After reset RAM memory always is in undefined state. All the variables declared initialized or not initialized, constants have to be store in the RAM memory to work program correctly. This all work done by the some software code that is inserted by the compiler before the main() routine. This small software code copies all the read write data from memory to ram memory. For variables memory allocation will be in the form as shown in figure below:


After flash memory RAM memory starts.  In ram memory firstly the constant, initialized static, global variable are allocated then uninitialized or zero initialize (ZI) variables are allocated to ZI data section. The small software code also zeroes all the ram location after ZI section.

After this LOCAL variables are allocated to RAM memory. Local variable can be allocated in the stack memory and Heap memory according to declaration of variables. If variables are declared using Dynamic memory allocation then variables allocated in the heap memory. Stack memory starts after the ZI section and grows down to lower memory location.
Heap memory starts lower memory RAM location and grows in up direction.

In this case there may be case when stack and heap memory collides. If this memory collision occurs then program may work abruptly.  When we dynamically allocate memory using malloc() or alloc() it will return a pointer variable which tell the memory availability. If the memory is available then it will return the pointer to allocated memory location. If memory is not free it will return a NULL value.