Dynamic and Static Allocation of Memory
The golden rule of computers states that anything and everything (data or instruction) that needs to be processed must be loaded into internal memory before its processing takes place. Therefore, every data and instruction that is being executed must be allocated some area in the main (internal) memory. The main (internal) memory is allocated in two ways: statically and dynamically.
Static Memory Allocation
When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as static memory allocation. For instance, when you declare a variable as follows:
int a;
int a[20];
Then, in such a case the computer knows that what the length of int variable is. Generally, it is 2 bytes. So, a chunk of 2 bytes internal memory will be allocated for variable “a” during compilation itself. Thus, it is an example of static memory allocation.
Dynamic Memory Allocation
When the amount of memory to be allocated is not known beforehand rather it is required to allocate (main) memory as and when required during runtime (when the program is actually executing) itself, then, the allocation of memory at run time is referred to as dynamic memory allocation.
Pointers
Def: A pointer variable is a variable that can store the address of another variable of its own type.
Integer pointer variable
An integer pointer variable is a pointer variable that can store the address of another variable of type integer.
Float pointer variable
A float pointer variable is a pointer variable that can store the address of another variable of type float.
Character pointer variable.
A character pointer variable is a pointer variable that can store address of another variable of type character.
Structure type pointer variable.
A structure pointer variable is a pointer variable that can store address of another variable of type structure.
While dealing with pointers we use two special operators
&:
address of operator / reference operator
*:
value at operator / de reference operator
Declaration of pointer variable
Syntax:
datatype *pointervariable;
Example:
int *p;
char *c;
char *name;
int *abc;
float *d;
Example:
int a=10;
int *p;
p=&a;
a: “a” is a normal int variable which can store an integer value
p: “p” is an integer type pointer variable so it can store the address of another variable of type integer.
Thus we can write
P=&a;