Wednesday 25 October 2017

Memory Allocation Notes

/* static storage class program example */

#include <stdio.h>
#define MAXNUM 3
void sum_up(void);
int  main()
{
    int count;
    printf("\n*****static storage*****\n");
    printf("Key in 3 numbers to be summed ");
    for(count = 0; count < MAXNUM; count++)
        sum_up();
    printf("\n*****COMPLETED*****\n");

return 0;
}
void sum_up(void)
{
    /* at compile time, sum is initialized to 0 */
    static int sum = 0;
    int num;
    printf("\nEnter a number: ");
    scanf("%d", &num);
    sum += num;

    printf("\nThe current total is: %d\n", sum);

}

/************************Register*********************/

  1. ·        Register keyword is used to define local variable.
  2. ·        Local variable are stored in register instead of RAM.
  3. ·        As variable is stored in register, the Maximum size of variable = Maximum Size of Register
  4. ·        unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register.
  5. ·        This is generally used for faster access.
  6. ·        Common use is “Counter“.


Syntax
{
register int count;
}
Register storage classes example
#include<stdio.h>

void  main()
{
int num1,num2;
register int sum;

printf("\nEnter the Number 1 : ");
scanf("%d",&num1);

printf("\nEnter the Number 2 : ");
scanf("%d",&num2);

sum = num1 + num2;

printf("\nSum of Numbers : %d",sum);

getch();
}

/***********Automatic Storage Classes and Variables**********/

This is default storage class
All variables declared are of type Auto by default 
In order to Explicit declaration of variable use ‘auto’ keyword
auto int num1 ;   // Explicit Declaration 
Features :
 Storage
Memory
 Scope
Local / Block Scope
 Life time
Exists as long as Control remains in the block
 Default initial Value
Garbage

Example :
#include<stdio.h>
#include<conio.h>
void main()
{
 auto mum = 20 ;
 {
      auto num = 60 ;
             printf("nNum : %d",num);
 }
 printf("nNum : %d",num);
}

Output :
Num : 60
Num : 20


/**********Malloc*********/
*C program to create memory for int, char and float variable at run time.*/

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

int main()
{
    int *iVar;
    char *cVar;
    float *fVar;
     
    /*allocating memory dynamically*/
     
    iVar=(int*)malloc(1*sizeof(int));
    cVar=(char*)malloc(1*sizeof(char));
    fVar=(float*)malloc(1*sizeof(float));
     
    printf("Enter integer value: ");
    scanf("%d",iVar);
     
    printf("Enter character value: ");
    scanf(" %c",cVar);
     
    printf("Enter float value: ");
    scanf("%f",fVar);
     
    printf("Inputted value are: %d, %c, %.2f\n",*iVar,*cVar,*fVar);
     
    /*free allocated memory*/
    free(iVar);
    free(cVar);
    free(fVar);

    return 0;
}
OUTPUT:-
    Enter integer value: 100
    Enter character value: x
    Enter float value: 123.45
    Inputted value are: 100, x, 123.45



2 comments:

  1. hi sir,
    please upload BCS-042-Analysis And Design Of Algorithm &
    BCSL-045-Analysis And Design Of Algorithms Lab solved assignments asap...

    ReplyDelete
  2. hi sir
    please upload BCS-042-Analysis And Design Of Algorithm &
    BCSL-045-Analysis And Design Of Algorithms Lab solved assigments for BCA 4th Semester .

    Thanks

    ReplyDelete