Upload
jacob-williams
View
212
Download
0
Embed Size (px)
Citation preview
Introduction of
Memory Allocation
Memory Allocation
There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time or Dynamic allocation (using pointer)
Compile-Time
In the first type of allocations, the required amount of memory is allocated to the program element at the start of the program.
Here, the memory to be allocated to the variable is fixed and is determined by the compiler at the compile time.
Compile-Time
If it is a single integer variable it allocates two bytes to it.
If it is an array of five integer values it allocates ten bytes to it.
If it is single float type of variable compiler allocation four bytes to it.
Compile-Time
For example:int x ,y; //x & y variable allocated 2 bytesfloat a[5]; // 5*4=20 bytes for float array.
Here, array variable stored in consecutive (sequential) memory location & other variable are stored randomly at any unknown location in memory.
Compile-Time
The main problem with static memory allocation is that if you store less number of elements then the number of elements for which you have declared memory, then the rest of memory will be wasted. so in C, it is not be made available to other application and status is set as allocated and not free.
This leads to the inefficient use of memory.
Dynamic Allocation
The concept of dynamic allocation helps us to overcome this problem in arrays, as will as allows us to be able to get the required chunk of memory at run-time.
This is best suited type of allocation where we do not know the memory requirement in advance, which is the case with most of real-time problem.
Dynamic Allocation
In other words, dynamic memory allocation gives flexibility for programmer.
As well as it make efficient use of memory, by allocating the required amount of memory whenever needed unlike static allocation where we declare the amount to be allocated statically.
Dynamic Allocation
C provide following dynamic allocation and de-allocation functions: malloc() calloc() free() realloc()
Dynamic Allocation
C++ provide following dynamic allocation and de-allocation Operators are: new delete
Memory Management Operators: new
In C++ supports dynamically memory allocation and free the memory allocation by using two unary operators new and delete that perform the task in better and easier way.
An object can be created by using new and destroyed by using delete as and when required.
A data object created inside a block with new will remain in existence until it its explicitly destroyed by using delete.
Memory Management Operators: new
The new operator can be used to create objects of any type.
It takes the following general form:
pointer variable=new data-type Here, pointer-variable is a pointer of type data-
type. The new operator allocates sufficient memory to
hold a data object of type data-type and returns the address of the object.
Memory Management Operators: new
The data-type may be any valid data type. The pointer variable hold the address of the
memory space allocated examples:
int *p=new int;
flaot *q=new float; Where p is a pointer of type int and q is a pointer
of type float.
Memory Management Operators: new
The new can be used to create a memory space for any data type including user-defined type such as arrays, structures and classes.
General form of the one-dimensional array is:
pointer-variable= new data-type[size] Here size specifies the number of elements in the
array.
Memory Management Operators: new
For example the statement :int *p=new int[10];
Creates a memory space for an array of 10 integer.
P[0] will refer to the first element,p[1] to second element and so on.
When creating multi-dimension arrays with new all the array sizes must by supplied:int *p=new int[10][5];int *q=new int[5][3][2];
Memory Management Operators: delete
When a data object is no longer needed, it is destroyed to realease the memory space for reuse.
The general form of its use is:
delete pointer-variable; The pointer-variable is the pointer that points to a
data object created with new. Examples:
delete p;
delete q;
Memory Management Operators: delete
If we want to free a dynamically allocated array, we must use the following form of delete:
delete [size] pointer-variable; The size specifies the number of elements in the
array to be freed. The problem with this form is that the
programmer should remember the size of the array.
Memory Management Operators: delete
For example:
delete []p; Through this statement it will delete the entire
array pointed to by p.