Skip to main content

Dynamic Memory Allocation

DMA(Dynamic Memory Allocation):-
                          At run time decision is taken how much memory is taken by the variable.So there are two ways come to make a variable .

  1. SMA:in which create a variable and how much memory taken is decided at the compilation time.For example :- int a, int b[10]; These variable a and b take a fix amount of memory.
  2. DMA:- second we create a variable which take the memory at the run time(i.e dynamically).The variable which is created dynamically does not have a name they have a address for which we use a pointer because only pointer access the address.
Use(need) of DMA:- 
                      we study about the array.An array is a collection of items(data) having a fixed size.
suppose we create an array.

 For Example:- int a[6]={1,2,3,4,5,6};

Here size of array is fixed.what will happen if we need to store more data in the array.we cannot do that.

another Example:- int b[6]={1,4,6};

Here is the size of array is 6 but memory occupy only 3 , 3 is blank which is the wastage of memory.

That's why DMA is used.Because due to dynamically we can store any number of data in the array.If array is fill and a new element is stored in array dynamically then the size of array also increased and due to DMA there is no Wastage of memory.

C provide 4 library function to achieve the DMA(Dynamic Memory Allocation).The definition of these function is present in header file stdlib.h
  1. malloc()
  2. calloc()
  3. free()
  4. realloc()
1)malloc or memory allocation:- it is used to allocate a large block of memory with the specified size.It returns a pointer of type void which can be casted(convert) into a pointer of any form.
Note:- if ptr==NULL; then memory is not allocate ,it is same for all 4 functions and good to use every time.

EXAMPLE:-


2)calloc or contiguous allocation:- it is used to allocate specified number of block memory of a specified type.It initialize each block with the default value 0.



EXAMPLE:-

Write the same program of malloc for calloc also but the only difference is in the line 10.In calloc program the line 10 is written as 

OUTPUT IS ALSO SAME


3)free:- the malloc and calloc are used for allocate the memory.So for deallocate that memory which create by malloc or by calloc, free is used.
Syntax:- free(ptr);

EXAMPLE:- 


NOTE:- Every time use free when ever you allocate a memory dynamically

4)realloc or reallocation:- if the dynamically allocated memory is insufficient then you can change the size of previously allocated memory is by using realloc.realloc means again allocate the memory if there is problem.


"""PLEASE SHARE WITH YOUR FRIENDS"""

Comments

Popular Posts

Type casting

type casting or type conversion  allows us to convert one data types into another.for type casting cast operator is required. Syntax:--      (type)expression or value; Example:       int f =9/4; //without type casting       o/p=2       float f=(float)9/4;  //with type casting       o/p=2.3 Type conversion are of two types: 1)Implicit Type Conversion:-  it is also known as automatic type conversion.When the type conversion is performed automatically by the compiler,such type of conversion is known as implicit type conversion. 2)Explicit Type Conversion:-  the type conversion performed by the programmer is known as explicit type conversion.which is done by cast operator as above mentioned.

CALL BY VALUE AND REFERENCE--

A function can be called by two types 1) Call By Value:-   when an original value is not modified , is known as call by value. In call by value the actual parameter(the parameter passed to function) is copied into the formal parameter(parameter receive by function). in this the value of each variable in calling function is copied into the corresponding dummy variable of the called function. the changes made to the dummy variable have no effect on the actual variable. Example:-   2)Call By Reference:-   when an original value is modified, is known as call by reference.in this a reference is used , denoted by &(symbol). we can use either pointer or reference for call by reference in this the address of the variable is passed into the function called by actual parameter. in this the memory allocation is same for both actual and formal arguments. Example:-   

UNION

Definition:-                 Union is similar to structure, except it allows you to define the variable that share storage spaces.Defining a means creating a new data types. How use union:-                       when we create a structure by using struct keyword, now making a union we just replace the keyword struct with the keyword union , other syntax will be same. Difference  between Structure  and Union:- the only difference between them is in memory. NOTE:-  union is used for low level programming but structure mostly used for high level programming. Program to access union:- """SHARE IT WITH YOUR FRIENDS IF HE DID NOT KNOW""""