0% found this document useful (0 votes)
22 views14 pages

Memory Management

Uploaded by

theeeclipse17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Memory Management

Uploaded by

theeeclipse17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Memory Management

Memory
• A memory or store is required in a computer to store
programs (or information or data).
• Data used by the variables in a program is also loaded
into memory for fast access.
• A memory is made up of a large number of cells, where
each cell is capable of storing one bit.
• The cells may be organized as a set of addressable
words, each word storing a sequence of bits
• These addressable memory cells should be managed
effectively to increase its utilization.
2.1. MEMORY ALLOCATION IN C
• There are two types of memory allocations in C:
• 1. Static memory allocation or Compile time
• 2. Dynamic memory allocation or Run time
• In static or compile time memory allocations, the
required memory is allocated to the variables at the
beginning of the program.
• Here the memory to be allocated is fixed and is
determined by the compiler at the compile time itself.
• For example,
• int i, j; //Two bytes per (total 2) integer variables
• float a[5], f; //Four bytes per (total 6) floating point variables
2.1. MEMORY ALLOCATION IN C
• When the first statement is compiled, two bytes for both
the variable ‘i’ and ‘j’ will be allocated.
• Second statement will allocate 20 bytes to the array A
[5 elements of floating point type, i.e., 5 × 4] and four
bytes for the variable ‘f ’
Drawbacks static memory allocation

• If you try to read 15 elements, of an array whose size is


declared as 10, then first 10 values and other five
consecutive unknown random memory values will be read.
• To assign values to 15 elements of an array whose size is
declared as 10, then first 10 elements can be assigned and
the other 5 elements cannot be assigned/accessed.
• The second problem with static memory allocation is that if
you store less number of elements than the number of
elements for which you have declared memory, and then
the rest of the memory will be wasted.
• That is the unused memory cells are not made available
The dynamic or run time memory
• It makes efficient use of memory by allocating the
required amount of memory whenever is needed.
• In most of the real time problems, we cannot predict the
memory requirements.
• Dynamic memory allocation does the job at run time.
• C provides the following dynamic allocation and de-
allocation functions :
• (i) malloc( ) (ii) calloc( )
• (iii) realloc( ) (iv) free( )
ALLOCATING A BLOCK OF MEMORY
• The malloc( ) function is used to allocate a block of memory in bytes.
• The malloc function returns a pointer of any specified data type after allocating a block of memory
of specified size.
• It is of the form
• ptr = (int_type *) malloc (block_size)
• ‘ptr’ is a pointer of any type ‘int_type’ byte size is the allocated
area of memory block.
• For example
• ptr = (int *) malloc (10 * sizeof (int));
• On execution of this statement, 10 times memory space equivalent to size of an ‘int’ byte is
allocated and the address of the first byte is assigned to the pointer variable ‘ptr’ of type ‘int’.
• The malloc() function allocates a block of contiguous bytes.
• The allocation can fail if the space in the heap is not sufficient to satisfy the request. If it fails, it
returns a NULL pointer.
• So it is always better to check whether the memory allocation is successful or not before we use
the newly allocated memory pointer.
• Next program will illustrate the same. PROGRAM 2.1
Structure variables
• Similarly, memory can be allocated to structure variables. For example
struct Employee
{
int Emp_Code;
char Emp_Name[50];
float Emp_Salary;
};
• Here the structure is been defined with three variables.
• struct Employee *str_ptr;
• str_ptr = (struct Employee *) malloc(sizeof (struct Employee));
• When this statement is executed, a contiguous block of memory of size
56 bytes (2 bytes for integer employee code, 50 bytes for character
type Employee Name and 4 bytes for floating point type Employee
Salary) will be allocated
2. ALLOCATING MULTIPLE BLOCKS OF MEMORY
• The calloc() function works exactly similar to malloc() function except for the fact that it
needs two arguments as against one argument required by malloc() function.
• While malloc() function allocates a single block of memory space, calloc() function allocates
multiple blocks of memory, each of the same size, and then sets all bytes to zero. The
general form of calloc() function is
• ptr = (int_type*) calloc(n sizeof (block_size));
• ptr = (int_type*) malloc(n* (sizeof (block_size));
• The above statement allocates contiguous space for ‘n’ blocks, each of size of block_size
bytes.
• All bytes are initialized to zero and a pointer to the first byte of the allocated memory block
is returned. If there is no sufficient memory space, a NULL pointer is returned.
• For example
• ptr = (int *) calloc(25, 4);
• ptr = (int *) calloc(25,sizeof (float));
• Here, in the first statement the size of data type in byte for which allocation is to be made
(4 bytes for a floating point numbers) is specified and 25 specifies the number of elements
for which allocation is to be made.
• Note : The memory allocated using malloc() function contains garbage values, the memory
allocated by calloc() function contains the value zero.
4. RESIZE THE SIZE OF A MEMORY BLOCK

. In some situations, the previously allocated memory is insufficient to run the


correct application, i.e., we want to increase the memory space.

It is also possible that the memory allocated is much larger than necessary,
i.e., we want to reduce the memory space.

In both the cases we want to change the size of the allocated memory block
and this can be done by realloc() function.

This process is called reallocation of the memory. The syntax of this function is
• ptr = realloc(ptr, New_Size)

Where ‘ptr’ is a pointer holding the starting address of the allocated memory
block.

And New_Size is the size in bytes that the system is going to reallocate.

Following example will elaborate the concept of reallocation of memory.

ptr = (int *) malloc(sizeof (int));


2. DYNAMIC MEMORY ALLOCATION
IN C++
• . Although C++ supports all the functions (i.e., malloc, calloc, realloc and free) used
in C, it also defines two unary operators new and delete that performs the task of
allocating and freeing the memory in a better and easier way.
• An object (or variable) 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 is
explicitly destroyed by using delete. Thus, the lifetime of an object is directly under
our control and is unrelated to the block structure of the program.
• 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. The data_type may be any valid
data type
• The Pointer_Variable holds the address of the memory space allocated.
• For example:
• int *Var1 = new int;
• float *Var2 = new float;
• Where Var1 is a pointer of type int and Var2 is a pointer of type float.
• When a data object is no longer needed, it is destroyed to release 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.
• delete Var1;
• delete Var2
FREE STORAGE LIST
• Freeing storage is not as easy as allocation. When a program
or block of program (or function or module) ends, the storage
allocated at the beginning of the program will be freed.
• Dynamically a memory cell can be freed using the operator
delete in C++.
• In memory a special list of unused (including the deallocated)
memory cells is maintained to provide (or allocate) memory
space.
• This list, which has its own pointer, is called the list of
available space or the free storage list or the free pool.
• Two problems arise in the context of storage release.
accumulation of
• Garbage (called garbage collection) and
• Dangling reference,
GARBAGE COLLECTION
• The garbage collection may take place when there is
only some minimum amount of space or no space at all
left
• in the free-storage list, or when the CPU is idle and has
time to do the collection.
• Generally speaking, the garbage collection is invisible to
the programmer

You might also like