Loading...
Loading...
00:00:00

What Is SMA and DMA In C?

SMA(Static Memory Allocation)

In c programming language, static memory allocation compiler decides the allocation of memory at compile time(before running the program) for variables . When program is running all variables gets their memory according to their declaration and remain from start to end of program. Once memory is allocated we can't change their size.

Examples of Static memory Allocation

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

int main(int argc, char const *argv[])
{
    //static memory allocation
    int num = 200;
    char ch = 'C';
    printf("The value of num is %d and the value of ch is %c\n", num, ch);
    return 0;
}

What Is DMA(Dynamic Memory Allocation)?

In c programming language, Dynamic memory allocation is used to allocate the memory in program at run time. Using Dynamic Memory Allocation we can grow and shrink the size of array(in Dynamic Arrays). Dynamic allocated memory can be access only using pointer because it has only address of that memory location. C standard library provide these function for memory management - malloc(), realloc(), calloc()and free() functions.

Syntax Of Malloc() Function

malloc() function takes size of memory block and returns the address of allocated memory. Allocated memory block will be type void so we have to type cast into int, float, char or structure according to uses.

void * malloc(size_t _Size)

Example Of Malloc() Function

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

int main(int argc, char const *argv[])
{
    //dynamic memory allocation
    int *num;                                  // pointer variable
    num = (int*)malloc(sizeof(int));           // pass size of int type block
    if (num == NULL)              // checking memory
        printf("Memory not initialized\n");
    *num = 45;                                 //assign value
    printf("The value of num is %d \n", *num); // access value
    free(num);                         // release memory
    return 0;
}

Calloc() Function

calloc() function allocates the memory Dynamically and fill the value with zero. It takes two arguments first is number_of_elements and second is size of _SizeOfElements. It returns pointer to the allocated block or NULL if memory not initialized.

Syntax of calloc() function

void *calloc(size_t _NumOfElements, size_t _SizeOfElements)

Example Of Calloc() Function

Using calloc() function, Create an array Dynamically.

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

int main(int argc, char const *argv[])
{
    //dynamic memory allocation
    int *arr;                     // pointer variable
    arr = calloc(3, sizeof(int)); // initialize memory
    if (arr == NULL)              // checking memory
        printf("Memory not initialized\n");
    arr[0] = 10;                  // initialize value
    arr[1] = 30;
    arr[2] = 70;
    printf("arr[0] is %d \n", arr[0]); // access value
    free(arr);                         // release memory
    return 0;
}

Realloc() Function

realloc() function is used to reallocate the previous dynamically allocated memory. Using realloc() function we can resize the memory. realloc() function returns the pointer of newly created memory and copy the all elements of old memory block.If memory is not reallocated returns NULL. realloc() function takes two arguments first pointer and second sizeof memory.

Syntax of realloc() function

void *realloc(void *_Memory, size_t _NewSize)

Example Of Realloc() Function

In this Example, we are extending the previously allocated array size using realloc().

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

int main(int argc, char const *argv[])
{
    //dynamic memory allocation
    int *arr;                     // pointer variable
    arr = calloc(3, sizeof(int)); // initialize memory
    if (arr == NULL)              // checking memory
        printf("Memory not initialized\n");
    arr[0] = 10; // initialize value
    arr[1] = 30;
    arr[2] = 70;
    realloc(arr, sizeof(5)); // extend the previous allocated memory block
    arr[3] = 35;
    arr[4] = 58;
    for (int i = 0; i < 5; i++)
        printf("arr[%d] is %d \n", i, arr[i]); // access value
    free(arr);                                 // release memory
    return 0;
}

Free() Function

free() is used to deallocate the previously allocated memory using malloc(), calloc(), realloc() functions. free() function takes pointer as an argument for deallocation. SMA type variable automatically deallocated memory after functions end but for the dynamically allocated variables we have explicitly mention for deallocation using free() function.

If you have noticed the above examples we have used free() function. Example of free() function

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

int main(int argc, char const *argv[])
{
    //dynamic memory allocation
    int *num;                  // pointer variable
    num = malloc(sizeof(int)); // initialize memory
    if (num == NULL)           // checking memory
        printf("Memory not initialized\n");
    *num = 20;
    printf("The value of num is %d\n", *num);
    free(num); // release memory
    return 0;
}