/*
* Sanfoundry.com Memory Leak Program – India Internship Bangalore
* Author: Manish Kumar Bhojasia, Location: Bangalore, India
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KB1 1024
#define MEMSIZE (4 * KB1)
char *string = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
char *gstring = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
char *gptr;
int num;
char * f1_alloc(void);
char * f2_copyme(void);
void f3_free(char *);
main()
{
srand((int)getpid());
while (1) {
gptr = f1_alloc();
strcpy(gptr, string);
f2_copyme();
f3_free(gptr);
}
}
char *f1_alloc()
{
return (malloc(MEMSIZE));
}
char *f2_copyme()
{
static int len, i = 0;
char *s;
len = strlen(string);
s = (char *)malloc(len);
strncpy(s, string, len);
i++;
if (!(i % MEMSIZE))
printf("i=%d, %s\n", i, s);
}
void f3_free(char *p)
{
num = rand() % 3;
if (!num)
free(p);
}
This program will hang your computer or forcibly terminated.
Error’s in this program are::
1.
LINE No 11-> the program has used macro’s to
define MEMSIZE (4*KB) which is a very large value and in whole program it will
not be utilised and it just a wastage of memory.
2.
LINE No 13-> char * string is initialized
with a string but this is not a good practice to store a address of a string in
a pointer without declaring the size of the pointer and it can cause
overwriting of memory.
3.
LINE No 14-> char * string is initialized
with a string but this is not a good practice to store a address of a string in
a pointer without declaring the size of the pointer and it can cause
overwriting of memory.
4.
LINE No15-> will show a logical error as loop
is declared for infinite times.
5.
LINE No35->Memory allocating many numbers of
times but de allocation is very rarely.
6.
LINE No
39-> “len” is declared as static and its value is overwrite again and again
which is not required when we had declared it as static.
7.
LINE No43-> in this “s” is allocated with
memory every time the function is called but de allocation is not there.
8.
“MEMSIZE” is replaced by a huge memory “4 * KB” but
most of the memory is wasted.
9.
Program generally shows “segmentation error”
because de allocation and allocation are not parallel.
10.
LINE 37-> In function “f2_copyme() has a
return type but function does not return any thing which can also be considered
as an error. http://www.sanfoundry.com/internship/

