Thursday 17 May 2018

One and Two Dimensional array using Dynamic Memory Allocation



Application's memory is divided into 4 segments - code, static/global, stack and heap

Stack memory for a program running is fixed at compile time by the compiler, os etc..
and some part of stack memory is allocated to main()  called stack frame.
the lifetime of local variables cannot be changed. 
If your program is using more memory than the stack memory then stack overflow occurs as stack will not grow.

If u want more memory at run time and u want to control the lifetime of a variable then use heap memory
Using heap memory is called dynamic memory allocation.

Dynamic memory allocation in C is done using 3 inbuilt functions in stdlib.h
  1. malloc() :  number of elements * size of one element    int *p = malloc(10​*sizeof(int)); does not initialize
  2. calloc() : int *p = calloc(10​,sizeof(int)); sets all values to zeros
  3. realloc() : pointer to existing block , size of new block ..int q=realloc(p,20*​sizeof(int));
Deallocation:
free(p);

Q)Declaration 1D array of size 3 using DMA

Solution :
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    return 0;
}

Q) Declaration 2D array of size 5x6 using DMA 

Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int r=5,c=6,i, j, count=1;
    int *arr[r];
    for (i=0; i<r; i++)
        arr[i]= (int *)malloc(c * sizeof(int *));
    for (i=0; i<r; i++)
        for (j=0; j<c; j++)
            arr[i][j] = count++;
    for (i=0; i<r; i++)
    {
        for (j=0; j<c; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
        
    return 0;
}


Q)Declaration of 2D array by taking row and column number using DMA

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int r,c,i, j, count=1;
    scanf("%d%d",&r,&c);
    int *arr[r];
    for (i=0; i<r; i++)
        arr[i]= (int *)malloc(c * sizeof(int *));
    for (i=0; i<r; i++)
        for (j=0; j<c; j++)
            arr[i][j] = count++;
    for (i=0; i<r; i++)
    {
        for (j=0; j<c; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
        
    return 0;
}

Q) Create an array of size 10 initially and read k value from user increase the array size by k

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j,k;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    
    scanf("%d",&k);
    printf("\n allocate extra %d memory\n",k);
    arr = (int *)realloc(arr, sizeof(int)*k);
    
    for(i=n;i<n+k;i++)
        arr[i]=i;
    for(i=0;i<n+k;i++)
        printf("%d\t",arr[i]);
    return 0;
}

Q) Declare an array of size k and read the elements into array and print those elements and free the memory occupied by array

Solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n=3,i, j;
    int *arr = (int *)malloc(n * sizeof(int *));
    for (i=0; i<n; i++)
         arr[i] = i;
    for (i=0; i<n; i++)
        printf("%d\t",arr[i]);
    free(arr);
    
    return 0;
}

No comments:

Post a Comment