3. Memory management is the process of controlling
and coordinating, computer memory assigning
portions called blocks to various running programs
to optimize overall system performance.
Memory management resides in hardware, in the
OS (operating system), and in programs and
applications.
MEMORY MANAGEMENT
4. Memory is allocated for the declared variable by the
compiler. The memory is allocated during compile
time. Since most of the declared variables have
static memory, this kind of assigning the address of
a variable to a pointer is known as static memory
allocation. The lifetime of a variable in static
memory is the lifetime of the program.
STATIC MEMORY ALLOCATION
5. Allocation of memory at the time of execution (run
time) is known as dynamic memory allocation.
Dynamic allocation is achieved using certain
functions like malloc(), calloc(), realloc(), free in C
and operators like "new", "delete" in C++. Dynamic
allocation of memory space is done by using these
functions when value is returned by functions and
assigned to pointer variables.
DYNAMIC MEMORY ALLOCATION
6. The new operator denotes a request for memory
allocation at run time. If sufficient memory is
available, new operator initializes the memory and
returns the address of the newly allocated and
initialized memory to the pointer variable.
Syntax to use new operator:
pointer-variable = new data-type;
int *p;
p = new int;
NEW OPERATOR
7. Initialize memory:We can initialize the memory using
new operator.
pointer-variable = new data-type(value);
Example:
int *p = new int(25);
float *p = new float(75.25);
Char *p=new char(‘A’);
NEW OPERATOR (CONTINUE)
8. Combine declaration of pointer and their assignment
int *p = new int;
Example:
int *p = new int;
float *p = new float;
NEW OPERATOR (CONTINUE)
9. Allocate block of memory: new operator is also used to
allocate a block(an array) of memory of type data-
type.
pointer-variable = new data-type[size];
int *p = new int[10];
Dynamically allocates memory for 10 integers continuously of
type int and returns pointer to the first element of the
sequence, which is assigned to p(a pointer). p[0] refers to first
element,p[1] refers to second element and so on.
NEW OPERATOR (CONTINUE)
10. When memory allocated by new operator is no longer
required, it is freed using operator delete.
Syntax:
delete pointer-variable;
Here, pointer-variable is the pointer that points to the
data object created by new.
Examples:
delete p;
delete q;
DELETE OPERATOR
11. To free the dynamically allocated array pointed by
pointer-variable, use following form of delete:
delete[] pointer-variable;
Example:
delete[] p;
The brackets [] indicates the array has been deleted. If
you need to delete a single object then, you don't need
to use brackets.
DELETE OPERATOR (CONTINUE)
12. #include <iostream>
using namespace std;
class Box {
public:
Box() {
cout <<“box"<<endl; }
};
int main( ) {
Box* myBoxArray = new Box[4]; // Box memory
allocation
delete[] myBoxArray; // Box memory is released or
delete
return 0;
}
OUTPUT:
box
box
box
box
13. #include <iostream>
using namespace std;
int main ()
{
int i,n,total=0;
int * p;
cout << "How many subjects would you like to type? ";
cin >> i;
p= new int[i];
for (n=0; n<i; n++)
{
cout <<endl<<"Enter marks in subject "<<1+n<<":";
cin >>*(p+n); or cin>>p[i];
}
for (n=0; n<i; n++){
total=total+*(p+n); or total=total+p[i];
}
cout<<endl<<"Total:"<<total;
cout<<endl<<endl<<"Average:"<<total/i;
delete[] p;
return 0;
}