Tuesday 28 February 2012

C Program to show Dynamic Memory Allocation using Malloc Function

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int no, *pt,i;
    clrscr();
    printf("Enter no of Students :");
    scanf("%d",&no);
    pt=(int *)malloc(no*2);
    if(pt== NULL)
    {
        printf("\n\nMemory allocation failed!");
        getch();
        exit(0);
    }
    printf("* * * * Enter roll no of students. * * * *\n");
    for (i=0;i<no;i++)
    {
        printf("-->");
        scanf("%d",(pt+i));
    }
    printf("\n* * * * Entered roll no. * * * *\n");
    for (i=0;i<no;i++)
    {
        printf("%d, ",*(pt+i));
    }
    getch();
}
 

Code Explanation:

Let’s dig out the above program; this is a simple c program example which uses integer pointer to dynamically allocate memory according to user’s input at runtime. As I told you earlier that we can do this either with malloc() or calloc(). In this example I am using malloc() and calloc() is explained below. To use malloc() or calloc() function we must include stdlib.h or alloc.h header file otherwise you will get malloc prototype error at compilation. Line no. 3 includes the stdlib.h header file. In line no. 7 an integer pointer pt and some other integer is declared which we will need later in input and loop. Line no. 11 stores integer number in no variable which is used as number of students whose roll number we are going to input. Next line (line no. 12) is most important one in this program; it does all allocation jobs. If malloc function is able to allocate memory then it returns address of memory chunk that was allocated else it returns NULL.
pt=(int *)malloc(no*2);
As you already know that malloc is a function, it requires one parameter or argument i.e. number of bytes to reserve. In our program we provided no*2 as parameter. The variable no holds the number of students whose roll no we are going to enter. But why we are multiplying no with 2? Here is answer, suppose we want to store roll number of 10 students. We stored 10 in no variable and passed this no variable in malloc function. But do you think it will work? Because integer occupies 2 bytes and we wanted to store 10 students information. And if we pass 10 to malloc then compiler will reserve 10 bytes only which can store only 5 students’ information. To solve this issue I have multiplied no with 2 so if you enter 10 then it will reserve 20 bytes of memory. If you don’t remember which data type occupies how much memory then you can use sizeof() which return data type size.
pt=(int *)malloc(no*sizeof(int));
Just before malloc() I have used (int *), this is called type casting (means converting from one data type to other compatible data type). If memory allocation is successful then malloc() by default returns pointer to a void and we type cast that to integer pointer and stores the address in pt integer pointer variable. So pt pointer contains starting address of memory allocated using malloc().
If, due any reason, malloc() fails to allocate memory it returns NULL. Line no. 13 – 18 does the same job, if malloc() fails to allocate memory the it will show error and exit from program.
Line no 21 – 25 is used to take roll no of student and store it in memory. Look at line no. 24 and particularly at (pt + i) statement. Integer pointer pt contains starting address of memory which is allocated by malloc() and by adding i variable value we are instructing it to move further to store value in that memory chunk. For example, if pointer starting address is 1002 then (pt+1) would mean 1004, (pt+2) = 1006 and so on.
Line no. 28 – 31 prints the value stored in the memory and thing is simple.

LAN CrossConnection Between Two Computers

Crossover cabling is essential if you want to share files between your laptop and desktop (basically two computers). Its very cheap to prepare a crossover able. You just need two RJ45 jacks, CAT5e cable (length depends on your requirement) and one crimping tool. Apart from these one more important thing is there, that is color coding which I am going to tell you in this article.
The process of crimping is so simple that, if carefully followed, a newbie can also do this with no difficulty. Because this is a crossover connection (i.e. PC to PC connection, no hub or switch is required) it is a bit confusing than direct LAN cabling. But, once you understand the color coding, it will become the easiest job in the world. So, pay attention & read carefully...