marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

Dynamic Memory Allocations in C


1) Malloc in C :

malloc() is used to allocate memory space in bytes for variables of any valid C data type.

Syntax :
pointer= (data_type*)malloc(user_defined_size);
Sample Program :
malloc.c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,*ptr;
a=10;
ptr=(int*)malloc(a*sizeof(int));
ptr=a;
printf("%d",ptr);
free(ptr);
getch();
}
Program Algorithm / Explanation :
  1. #include<stdio.h> header file is included because, the C in-built statementprintf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. stdlib.h is used because malloc() comes under it.
  4. int type variable a and pointer *ptr are declared.
  5. Variable a is assigned a value of 10.
  6. malloc() is used to allocate memory to pointer ptr.
  7. The size given through malloc() to ptr is sizeof(int).
  8. Now ptr is assigned the value of aptr=a; so the value 10 is assigned to ptr, for which, we dynamically allocated memory space using malloc.
  9. The value in ptr is displayed using printf
  10. Then allocated memory is freed using the C in-built function free().
Output :
malloc() in C

2) Calloc in C :

calloc() is sued to allocate multiple blocks of memory dynamically during the execution (run-time) of the program.
Syntax :
pointer=(data_type*)calloc(no of memory blocks, size of each block in bytes);
Sample Program :
calloc.c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *ptr,a[6]={1,2,3,4,5,6};
int i;
ptr=(int*)calloc(a[6]*sizeof(int),2);
for(i=0;i<7;i++)
{
printf("\n %d",*ptr+a[i]);
}

free(ptr);
getch();
}
Program Algorithm / Explanation :
  1. #include<stdio.h> header file is included because, the C in-built statementprintf we used in this program comes under stdio.h header files.
  2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files.
  3. stdlib.h is used because malloc() comes under it.
  4. A Pointer *ptr of type int and array a[6] is declared.
  5. Array a[6] is assigned with values.
  6. Another variable i of type int is declared.
  7. Calloc() is used to allocate memory for ptr6 blocks of memory is allocated with each block having 2 bytes of space.
  8. Now variable i is used in for to cycle the loop 6 times on incremental mode.
  9. On each cycle the data in allocated memory in ptr is printed using *ptr+a[i].
  10. Then the memory space is freed using free(ptr).
Output :
calloc() in C


No comments:

Post a Comment