- A memory is made up of 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.This is called memory Management.
- There are 2 types of memory allocations in C
- Static memory allocation or Compile time allocation
- Dynamic memory allocation or Run time allocation
- The required memory is allocated to the variables at the beginning of the program.
- The memory to be allocated is fixed and is determined by the compiler at the compile time itself.
when the first statement int a,b; is compiled 4 bytes(integer occupies 4 bytes in a 32 bit machine) for each variable a,b will be allocated. The statement float x,y; will allocate 4 bytes for the x variable and 20 bytes[5 element array, each has 4 bytes] for the array y.
- If we declare more number of elements and we are using less number of elements then the memory allocated for the not used variables will be wasted.The unused memory cells are not made available to other applications.This leads to inefficient use of memory.
Here y array is declared but not used in the program.So the memory allocated for the array is wasted.
- If we try to assign values to 10 elements of an array whose size is declared as 5,then first 5 elements can be assigned and the other 5 elements cannot be assigned.
- If we try to read 10 elements of an array whose size is declared as 5,then first 5 values are read from the array and the other 5 consequtive unknown random memory values will be read.
- In most of the real time problems, we cannot predict the memory requirements.
- It makes efficient use of memory by allocating the required amount of memory whenever is needed.
- dynamic memory allocation does the job at run time.
- C provides the following dynamic allocation and deallocation functions
- The malloc() function is used to allocate a block of memory in bytes dynamically during execution.
- This function requires the number of bytes to be allocated and returs the base address of the chunk of memory that it allocates.
- The address returned by malloc() is always of the type void*.Hence it is required to convert in to a required data type using typecastng.
- Example:Allocating memory space for n integers of int type
- 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 using the allocated memory.
- calloc functionworks similar to malloc() function except it needs 2 arguments and calloc() function allocates multiple blocks of memory each of the same size and then sets all bytes to zero.
This will 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.
- calloc(m,n) is equivalent to p=m*malloc(n);
- malloc() allocates a bytes of memory but calloc() allocates blocks of memory.
- malloc() takes a single argument(memory required in bytes) but calloc() takes 2 arguments (number of variables to allocate memory,size in bytes of a single variable)
- The memory allocated using malloc() function contains garbage values,the memory allocated by calloc() function contains the value 0.
- Sometimes we need to increase the memory space or we want to reduce the memory space.
- To change the size of the allocated memory block realloc() function is used.
ptr- >pointer holding the starting address of the allocated memory block
new_size=size in bytes
- free() function is used to deallocate the previously allocated memory using malloc() or calloc() function.
- It is important to release the memory block for future use when it is not in use,using free() function.
ptr is a pointer to a memory block which has already been allocated.
- Trying to release an invalid pointer may create problems and cause system crash.