Dynamic Memory Allocation (DMA)

The way toward assigning and liberating memory in a program at run time is known as dynamic memory allocation. The DMA procedure saves the memory required by the program and permits the program to use the memory. At the point, when distributed memory is no more being used, it restores the dispensed memory to the framework. 

Dynamic memory allocation is highly useful in situations where the required memory space for a variable is unknown. We can simply define a pointer to variable without defining how much memory is required and later, based on requirement, we can allocate memory.

i) malloc()

The function malloc() designates mentioned size of bytes and returns a pointer to the principal byte of the dispensed space to the program. Its language structure is: 

Example :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {

  char name[100];
  char *description;

  strcpy(name, "Webtrickshome Tutorials");

  /* allocate memory dynamically */
  if( description == NULL ) {
    fprintf(stderr, "Error - unable to allocate required memory\n");
  } else {
    description = malloc( 200 * sizeof(char) );
  }

  strcpy( description, "Webtrickshome | Web Design and Development Tutorials with videos, blogs and discussion forum | Learn Photoshop, HTML, CSS, JavaScript, C Programming, jQuery, PHP, SQL, Bootstrap, Laravel, Wordpress, OOPs from detailed lessons");

  printf("Website: %s\n", name );
  printf("Description: %s\n", description );
}

ii) calloc() 

The function calloc() gives access to the C memory stack, which is accessible for dynamic allotment of variable-sized squares of memory. 

Example :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {

  char name[100];
  char *description;

  strcpy(name, "Webtrickshome Tutorials");

  /* allocate memory dynamically */
  if( description == NULL ) {
    fprintf(stderr, "Error - unable to allocate required memory\n");
  } else {
    description = calloc(200, sizeof(char) );
  }

  strcpy( description, "Webtrickshome | Web Design and Development Tutorials with videos, blogs and discussion forum | Learn Photoshop, HTML, CSS, JavaScript, C Programming, jQuery, PHP, SQL, Bootstrap, Laravel, Wordpress, OOPs from detailed lessons");

  printf("Website: %s\n", name );
  printf("Description: %s\n", description );
}

iii) free()

The function free() releases a block of memory specified by address.

Example : 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {

   char name[100];
   char *description;

   strcpy(name, "Webtrickshome Tutorials");

   /* allocate memory dynamically */
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   } else {
     description = calloc(200, sizeof(char) );
   }

   strcpy( description, "Webtrickshome | Web Design and Development Tutorials with videos, blogs and discussion forum | Learn Photoshop, HTML, CSS, JavaScript, C Programming, jQuery, PHP, SQL, Bootstrap, Laravel, Wordpress, OOPs from detailed lessons");

   printf("Website: %s\n", name );
   printf("Description: %s\n", description );

   free(description);
}

iv) realloc()

The function realloc() is used to increase or decrease the size of an already allocated memory block.

Example :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {

  char name[100];
  char *description;

  strcpy(name, "Webtrickshome Tutorials");
  description = calloc(200, sizeof(char) );
  /* allocate memory dynamically */
  if( description == NULL ) {
    fprintf(stderr, "Error - unable to allocate required memory\n");
  } else {
    strcpy( description, "Webtrickshome | Web Design and Development Tutorials with videos, blogs and discussion forum | Learn Photoshop, HTML, CSS, JavaScript, C Programming, jQuery, PHP, SQL, Bootstrap, Laravel, Wordpress, OOPs from detailed lessons");
  }

  description = realloc( description, 300 * sizeof(char) );

  if( description == NULL ) {
    fprintf(stderr, "Error - unable to allocate required memory\n");
  } else {
    strcat( description, "\n\nDynamic Memory Allocation in C Programming");
  }

  printf("Website: %s\n", name );
  printf("Description: %s\n", description );

  free(description);
}

Utilization of Pointer in DMA

1) The functions utilized for allocation of memory at run time return the location of allotted memory utilizing pointer. 

2) Pointers can be utilized to pass data to and fro between a function and its reference point quickly. 

Structures and Dynamic Memory Allocation

Structure is the collection of different types of data stored in a single variable or a common heading name.

Syntax:

struct structure_name 
{
  member definition;
  member definition;  
  ……………………
  ……………………
  member definition;
}

Accessing Structure Members

After creating the structure variable, the member of the structure can be accessed as:

Example :

#include <stdio.h>

struct references
{
  char title[50];
  int edition;
  char publisher[50];
  char author[50];
};
int main()
{
  struct references Book1;        /* Declare Book1 of type references */
  struct references Book2;        /* Declare Book2 of type references */

  /* book 1 specification */
  strcpy( Book1.title, "A Book on C");
  Book1.edition = 4;
  strcpy( Book1.publisher, "Pearson Education");
  strcpy( Book1.author,"AL Kelley, Ira Pohl");

  /* book 2 specification */
  strcpy( Book2.title, "C: How to program");
  Book2.edition = 7;
  strcpy( Book2.publisher, "Deitel & Deitel");
  strcpy( Book2.author,"Pearson Education");

  /* print Book1 info */
  printf( "Book 1 title : %s\n", Book1.title);
  printf( "Book 1 edition : %d\n", Book1.edition);
  printf( "Book 1 publisher : %s\n", Book1.publisher);
  printf( "Book 1 author : %s\n", Book1.author);

  /* print Book2 info */
  printf( "Book 2 title : %s\n", Book2.title);
  printf( "Book 2 edition : %d\n", Book2.edition);
  printf( "Book 2 publisher : %s\n", Book2.publisher);
  printf( "Book 2 author : %s\n", Book2.author);

  return 0;
}

Array of structure

The collection of similar types of structure within a single variable is called array of structure.

Example : 

#include<stdio.h>

struct employee
{
  char name [20];
  int age;
  float salary;
}emp[100];
void main ()
{
  int i,n,j;
  printf("Enter the no of employees\n");
  scanf("%d",&n);
  printf("Enter employee info as name , age , salary\n");
  for(i=0;i<n;i++)
  {
    scanf("%s %d %f",emp[i].name,&emp[i].age,&emp[i].salary);
  }
  printf("\nEMP_NAME\tEMP_AGE\t\tEMP_SALARY\n");
  for(i=0;i<n;i++)
  {
    printf("%s\t\t%d\t\t%f\n",emp[i].name,emp[i].age,emp[i].salary);
  }
}

Passing structure to a function

Similar to the normal variable, structure variable can also be passed to a function as an argument. While passing the structure to a function, the declaration of structure must be done above the prototype declaration of function.

Example : 

#include<stdio.h>

struct numbers
{
  int d1, d2;
};
int add(d1,d2);
int main ()
{
  int d3;
  struct numbers d1,d2;
  printf("Enter two numbers \n");
  scanf("%d%d",&d1,&d2);

  d3 = add(d1,d2);
  printf("The sum of given numbers is : %d",d3);
  return 0;
}

int add(d1,d2)
{
  int sum;
  sum = d1 + d2;
  return sum;
}

Structure and Pointer 

The pointer variable is used to store the address of structure variable. To access the member of structure, pointer can be used as given below.

Example :

#include <stdio.h>
struct player
{
  char name[20];
  int age;
};

int main(){
  struct player *p, py;
  p = &py;//referencing

  strcpy( py.name, "Jonjoe Shelvey");
  py.age = 31;

  printf("\n The players name is %s and his age is %d",p[0].name,p[0].age);
}

Passing Structure By Reference

Structure data can be passed to a function by reference.

Example :

#include<stdio.h>

struct player
{
  char name [20];
  int age;
};
void getData(p);
void main ( )
{
  struct player *p, py;
  p = &py;
  printf("enter player name and age \n");
  scanf("%s%d", &p->name,&p->age);
  getData(p);

}

void getData (struct player p)
{
   printf("\nThe name of the player is %s and his age is %d.\n",p.name,p.age);
}

Union

Union is the collection of heterogenous data similar to structure. The difference between union and structure is the members of the union share the memory. Since, all  the union members share the same memory, storing values to all the members and displaying them simultaneously is not possible. However, assigning the values and displaying them one by one works fine.

Example :

#include<stdio.h>
union data
{
  int x;
  char y;
  float z;
};

void main ()
{
  union data u;
  u.x = 5;
  printf("Value of x is : %d\n",u.x);
  u.y = 'z';
  printf("Value of y is : %c\n",u.y,u.z);
  u.z = 10.5;
  printf("Value of z is : %f\t",u.z);
}

0 Like 0 Dislike 0 Comment Share

Leave a comment