stdlib

path: courses/c-course/stdlib.md

- **fileName**: stdlib
- **Created on**: 2024-07-10 18:58:02

stdlib is library in c for handle the allocate the memory and manage the memory and free it and refer to Dynamic memory allocation in c .

1- malloc :

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so that it has initialized each block with the default garbage value initially
****ptr = (int*) malloc(100 * sizeof(int));****

2- calloc() method

  1. “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. it is very much similar to malloc() but has two different points and these are:
  2. It initializes each block with a default value ‘0’.
  3. It has two parameters or arguments as compare to malloc().
    `ptr = (int*)calloc(n, sizeof(int));

3- free

“free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.
free(ptr)

4- realloc() method

“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.
ptr = realloc(ptr, newSize);

example:


#include <stdio.h>
#include <stdlib.h>
int main()
{
    // This pointer will hold the
    // base address of the block created
    int* ptr;
    int n, i;
    // Get the number of elements for the array
    n = 5;
    printf("Enter number of elements: %d\n", n);
    // Dynamically allocate memory using calloc()
    ptr = (int*)calloc(n, sizeof(int));
    // Check if the memory has been successfully
    // allocated by malloc or not
    if (ptr == NULL) {
        printf("Memory not allocated.\n");
        exit(0);
    }
    else {
        // Memory has been successfully allocated
        printf("Memory successfully allocated using calloc.\n");
        // Get the elements of the array
        for (i = 0; i < n; ++i) {
            ptr[i] = i + 1;
        }
        // Print the elements of the array
        printf("The elements of the array are: ");
        for (i = 0; i < n; ++i) {
            printf("%d, ", ptr[i]);
        }
        // Get the new size for the array
        n = 10;
        printf("\n\nEnter the new size of the array: %d\n", n);
        // Dynamically re-allocate memory using realloc()
        ptr = (int*)realloc(ptr, n * sizeof(int));
          if (ptr == NULL) {
          printf("Reallocation Failed\n");
          exit(0);
        }
        // Memory has been successfully allocated
        printf("Memory successfully re-allocated using realloc.\n");
        // Get the new elements of the array
        for (i = 5; i < n; ++i) {
            ptr[i] = i + 1;
        }
        // Print the elements of the array
        printf("The elements of the array are: ");
        for (i = 0; i < n; ++i) {
            printf("%d, ", ptr[i]);
        }
        free(ptr);
    }
    return 0;
}

difference between malloc and calloc:

S.No. malloc() calloc()
1. malloc() is a function that creates one block of memory of a fixed size. calloc() is a function that assigns a specified number of blocks of memory to a single variable.
2. malloc() only takes one argument calloc() takes two arguments.
3. malloc() is faster than calloc. calloc() is slower than malloc()
4. malloc() has high time efficiency calloc() has low time efficiency
5. malloc() is used to indicate memory allocation calloc() is used to indicate contiguous memory allocation
6. Syntax : void* malloc(size_t size); Syntax : void* calloc(size_t num, size_t size);
8. malloc() does not initialize the memory to zero calloc() initializes the memory to zero
9. malloc() does not add any extra memory overhead calloc() adds some extra memory overhead

continue:[[]]
before:keyword-static