DYNAMIC MEMORY MANAGEMENT

 MULTIPLE FILE COMPILATION:

 Multiple file compilation in C allows you to divide your program into multiple source files and compile them separately before linking them together to create the final executable file. This can help to make your code more modular and easier to manage.

 

   Create separate source files: Divide your program into separate source files, each containing a different set of functions or modules.

 

   Create header files: Create header files for each source file that contains declarations for the functions and data structures used in that source file.

 

Compile the source files: Compile each source file separately using a C compiler. This will produce object files (.o files) for each source file.

HEADER FILES:

Header files in C are files that contain function prototypes, macro definitions, and other declarations that are used by other source files in a program. Header files typically have a .h extension and are included in source files using the #include directive.

 

DYNAMIC MEMORY MANAGEMENT:

Dynamic memory management in C allows you to allocate and deallocate memory at runtime, as opposed to static memory allocation where memory is allocated at compile time.

 

void * calloc(int num,int size);

         Clear allocation -> no garbage value

         Allocates array number of elements of which size in bytes will be size.

 void free(void* address)

         The function releases a block of memory by specified address.

void* malloc(size, t_size);

Memory allocation

This function allocates an array of new bytes and leaves them uninitialized.

void* realloc(new size)

         Reallocation

         This is used to increase/expand the memory which has been already given.



Example 01:

//calloc in Pointers
/* the calloc pointer by default store the garbage value*/

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

int main()
{
    //void* calloc(size_t n,size_t size)
 
    int i,n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
    int *ptr=(int *)calloc(n,sizeof(int));
    /*  
    so in here we declared it as a integer so type casting as a integer in the calloc
    it should get the limit and store the value until the integer */
 
    if(ptr==NULL)
    {
        printf("Memory Not Available ...");
        exit(1);
    }
 
    for(i=0; i<n; i++) //
    {
        printf("Enter a integer : ");
        scanf("%d",ptr+i);//
    }
 
    for(i=0; i<n; i++) //
    {
        printf("\n%d :  %d  ",&ptr+i,*(ptr+i)); //
    }
 
    return 0;
}


Example 02:

//malloc in Pointers
#include<stdio.h>
#include<stdlib.h>
int main()
{
    //void* malloc(size_t size)

    int i,n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
 
    int *ptr=(int *)malloc(n*sizeof(int));
 
    if(ptr==NULL)
    {
          printf("Memory Not Available ...");
          exit(1);
    }
   
    for(i=0; i<n; i++)
    {
        printf("Enter a integer : ");
        scanf("%d",ptr+i);
    }
    for(i=0;i<n;i++)
    {
        printf("%d  ",*(ptr+i));  // ptr+1
    }
 
    return 0;
}


Example 03:

//realloc in pointers

//void* realloc ( void *ptr,size_t new_size )


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

int main() {

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

    if(ptr==NULL) {
        printf("Memory Not Available ...");
        exit(1);
    }

    printf("\nEnter 3 Nos : \n");
    for(i=0; i<3; i++) {
        scanf("%d",ptr+i);
    }

    /* by using realloc we can use the same */

    ptr=(int *) realloc(ptr,5*sizeof(int));
    for(i=3; i<5; i++) {
        scanf("%d",ptr+i);
    }

    for(i=0; i<5; i++) {
        printf("%d  ",*(ptr+i));
    }
    return 0;
}









Post a Comment

0 Comments