1. void main()
{
int *x, n,i;
printf(“Enter the number of elements”);
scanf(“%d”,&n);
x=(int *) malloc(n*sizeof(int));
for(i=0; i<n; i++)
{scanf(“%d”, (x+i));}
for(i=0; i<n; i++)
{
printf(“%d”, *(x+i));
}
getch();
}
malloc will allocate a big block of 2*n bytes. So let’s say if
n=5 than 10 byte memory will be allocated on run time
which will be further type casted as x (pointer variable
stores address of memory)
x 10 20 30 40 50
10 20 30 40 50
x+1 x+2 x+3 x+4x+0
1000 1002 1004 1006 1008
*(x+0) *(x+1) *(x+2) *(x+3) *(x+4)
2. Follow Discussion Further to see
explanation of the complete program
malloc() is no more a FEAR it’s a DEAR
3. Why Dynamic Memory Allocation?
• As you know we cant declare an array as int a[n], bacsue value
of n will be available at run time (by scanf) and this memory is
required during compilation of program.
• Therefore we need such a memory which can be allocated at
run time.
• C Language provides a predefined function named mallloc() in
header file <stdlib.h> and <stdio.h>as predefined function to
provide memory at run time i.e. Dynamic Memory Allocation.
4. Syntax of malloc
• malloc (int)
• You must be thinking that in other books we have seen a much typical
syntax of malloc() something like below:
• x=(int *) malloc(n*sizeof(int));
Denotes number of bytes should be
allocated by malloc() on run time.
5. Confused about malloc() Syntax ??
• Don’t worry malloc(int) is the only correct syntax.
• Now let me explain you what malloc() is doing.
• Basically in C Language Program even if you write
malloc(20) this line executes successfully without a
problem and allocates 20 bytes in RAM at run time
without any error.
6. malloc() syntax cont.
• Then question comes what about this big line which you
generally see in books as syntax of malloc.
• x=(int *) malloc(n*sizeof(int));
• Now let me shock this line has nothing to do with syntax
of malloc.
• To solve this mystery follow this discussion further.
7. Just Think?
• User is technical or non technical ?
• Obviously non technical, so being a non computer science
professional is he able to understand what is bytes?
• Obviously answer is NO, he has concern to his number of elements to
be stored just this!!!!!
• Now think about computer, does it understand number of elements
again answer is no, computer knows bytes and understands bytes
only.
8. Just Think cont.
• So now its programmer's responsibility to take number of
elements from user and to calculate the number of bytes to
be allocated.
• And so called difficult syntax given in most of the books is
doing nothing but implementing a simple unitary method to
convert number of elements to number of bytes.
10. datatype *ptr= (datatype *) malloc (number of elements* sizeof(datatype));
Giving us total amount of memory allocated by
malloc() simply by applying unitary method by
multiplying sizeof one element into total number
of elements.
Explicit typecasting for converting memory address from 10 bytes to 2 bytes so that
pointer arithmetic can work efficiently.
11. void main()
{
int *x, n,i;
printf(“Enter the number of elements”);
scanf(“%d”,&n);
x=(int *) malloc(n*sizeof(int));
for(i=0; i<n; i++)
{scanf(“%d”, (x+i));}
for(i=0; i<n; i++)
{
printf(“%d”, *(x+i));
}
getch();
}
malloc will allocate a big block of 2*n bytes. So let’s say if
n=5 than 10 byte memory will be allocated on run time
which will be further type casted as x (pointer variable
stores address of memory)
x 10 20 30 40 50
10 20 30 40 50
x+1 x+2 x+3 x+4x+0
1000 1002 1004 1006 1008
*(x+0) *(x+1) *(x+2) *(x+3) *(x+4)