-
Notifications
You must be signed in to change notification settings - Fork 0
/
memleak.c
63 lines (52 loc) · 1.27 KB
/
memleak.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char* allocMem1(long);
char* allocMem2(long);
void deallocMem(char*);
void fillItUp(long, char*);
int main()
{
printf("Hello, world!\n");
srand((unsigned int) time(NULL));
int i;
for(i=0 ; i<50 ; i++)
{
long randNum = (rand() * sizeof(char)) % 512;
char *memBlock;
if(i%2 == 0 && i%3 == 0)
{
memBlock = allocMem1(randNum);
fillItUp(randNum, memBlock);
continue;
}
else
{
memBlock = allocMem2(randNum);
fillItUp(randNum, memBlock);
}
deallocMem(memBlock);
}
return 0;
}
char * allocMem1(long size)
{
return (char *) malloc(size);
}
char * allocMem2(long size)
{
return (char *) malloc(size);
}
void deallocMem(char * ptrToMemBlock)
{
free(ptrToMemBlock);
return;
}
void fillItUp(long howManyBytes, char * theBlock)
{
int i;
for(i=0 ; i<howManyBytes; i++)
{
theBlock[i] = 'z';
}
}