Dynamic Memory
Allocation:
An ARRAY NAME is
actually a POINTER to the FIRST ELEMENT within the
array.
It is possible to
define the array as a pointer rather than as a conventional
Array.
A conventional array
results in the beginning of program execution.
This can be over
ruled if array is represented as a pointer variable.
A pointer variable
representing an array requires some type of initial
memory assignment
before the array elements are processed which is
called as DYNAMIC
MEMORY ALLOCATION.
The DYNAMIC MEMORY
ALLOCATION is handled using malloc() &
calloc() functions.
Whenever memory is
allocated dynamically, the processed data finds a
place in the HEAP
rather then STACK.
Hence the contents of
the heap should be freed immediately after operation,
for which free()
function is used.
malloc() &
calloc() functions are defined to return a pointer to char,
hence the pointer
that is returned should be type cast to the appropriate
pointer.
malloc() or calloc()
if fail, return a NULL, if success return the address of
the memory chunk that
was allocated.
Calloc() needs two
arguments i.e number of memory spaces and the data
type to be allocated,
and malloc() needs only one argument.
Stack v/s Heap
Allocation:
The memory allocated
as stack has unique name and address, therefore
memory location can
be accessed by name or through address.
The memory allocated
as heap does not contain a name, instead contains
only the starting
address of the memory allocated, therefore can be
accessed through this
address.
The memory allocated
through heap can be local or global respective to its
functional
definition.
If memory allocated
is global then that memory can be accessed through a
different function by
locating its address.
In static memory
allocation the memory is allocated and freed by system.
In dynamic memory
allocation it is the risk of the programmer.
Garbage Collection:
The process of
collection of memory locations, which are not in use and
freeing that space is
called as GARBAGE COLLECTION.
By default, memory
allocated by malloc() contains garbage values and that
allocated by calloc()
contains zeros.
<alloc.h>
prototype need to be declared when using the Dynamic Memory
Allocation.
Sort a
one-dimensional integer Array in ascending order using pointer notation:
Program
#include<stdio.h>
void main()
{
void sort(int, int *);
int k, n, *a;
printf("\nHow
Many Numbers Will Be Entered : ");
scanf("%d",
&n);
a = (int *) malloc(n *
sizeof(int));
for(k = 0; k < n;
k++)
{
printf("\nEnter
A[%d] Index Element : ", k);
scanf("%d",
a + k);
}
sort(n, a);
for (k = 0; k < n;
k++)
printf("\n\nThe
Sorted Data A[%d] : %d", k, *(a + k) );
free(a);
}
void sort(int n, int *a)
{
int k, j, temp;
for(k = 0; k < n -
1; k++)
{
for(j = k + 1; j <
n; j++)
{
if(*(a + j) < *(a
+ k))
{
temp = *(a + k);
*(a + k) = *(a + j);
*(a + j) = temp;
}
}
}
return;
}
Pointer and Strings:
Illustration 1
Program
#include<stdio.h>
void main()
{
char str[80], *p;
int len = 0;
p = str;
printf("\nEnter
The String To Find Its Length : ");
scanf("%s",
str);
while(*p != '\0')
{
p++;
len ++;
}
printf("\nThe
String is %s and its Length is : %d", str, len);
}
Illustration 2
Program
#include<stdio.h>
#define LEN 21
void main()
{
char *s1, *s2;
int flag = 1;
s1 = (char *)
malloc(LEN * sizeof(char));
s2 = (char *)
malloc(LEN * sizeof(char));
printf("\nEnter
The String 1 : "); scanf("%s", s1);
printf("\n\nEnter
The String 2 : "); scanf("%s", s2);
while(*s1 != '\0'
&& *s2 != '\0')
{
if(*s1 != *s2)
{
flag = 0; break;
}
s1++; s2++;
}
if(flag == 0)
printf("\nThe
Strings are Not Equal.");
else
printf("\nThe
Strings are Equal.");
}
Revisiting Sorting in
‘C’:
Insertion Sort
Program
#include<stdio.h>
#define N 5
void main()
{
int i, j, k, x, a[N];
printf("\nEnter
The Element To Be Inserted ( -9999 to stop) : ");
scanf("%d",
&x);
i = 0;
while(x != -9999
&& i < N)
{
k = i - 1;
while((x < a[k])
&& (k >= 0) )
{
a[k + 1] = a[k];
--k;
}
a[k + 1] = x;
printf("\n\nEnter
The Element To Be Inserted (To Stop Enter -9999) : ");
scanf("%d",
&x);
++i;
}
printf("\nThe
Final Sorted Array : ");
for(j = 0; j < i;
j++)
printf("%5d",
a[j]);
}
No comments:
Post a Comment