-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemManageV1.c
More file actions
222 lines (192 loc) · 4.9 KB
/
MemManageV1.c
File metadata and controls
222 lines (192 loc) · 4.9 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Allocation and logging of memory details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME "MemoryDetails.txt"
//function prototypes
void Alloc(void);
void Free(void);
void memAlloc(int **);
void memFree(int **);
//global variables
FILE *fp; //file pointer
int *ptrA,*ptrB,*ptrC; //demo pointers
unsigned memA,memB,memC; //allocated memory blocks
unsigned memory_alloc; //total memory allocated
int main()
{
int choice;
//opening and initializing file
fp = fopen(FILENAME,"w+");
if(fp == NULL)
{
printf("Cannot open file\n");
exit(0);
}
fputs(" Memory Details \n",fp);
fputs(" ____________________________________\n",fp);
fputs("| |\n",fp);
fputs("|Location | Blocks Allocated | Status|\n",fp);
fputs(" ------------------------------------ \n",fp);
fclose(fp);
while(1)
{
//main menu
printf("\n");
printf(" Mem Alocated -> %d Bytes \n",memory_alloc);
printf("|-------------------------------|\n");
printf("| 1. Allocate Some memory |\n");
printf("| 2. Free Some memory |\n");
printf("| 3. Exit |\n");
printf("|_______________________________|\n");
l1: printf("\nEnter your choice -> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
Alloc();
break;
case 2:
Free();
break;
case 3:
return 0;
default:
printf("Please enter correct choice\n");
goto l1;
}
}
return 0;
}
//function to choose which pointer you wanted to allocate memory
void Alloc(void)
{
int choice;
printf(" _______________________________\n");
printf("| |\n");
printf("| 1. Allocate memory to ptrA |\n");
printf("| 2. Allocate memory to ptrB |\n");
printf("| 3. Allocate memory to ptrC |\n");
printf("|_______________________________|\n");
l2: printf("\nEnter your choice -> ");
scanf("%d",&choice);
switch (choice)
{
case 1:
memAlloc(&ptrA);
break;
case 2:
memAlloc(&ptrB);
break;
case 3:
memAlloc(&ptrC);
break;
default:
printf("Please enter correct choice\n");
goto l2;
}
}
//function to choose which location to be freed
void Free(void)
{
int choice;
printf(" _______________________________\n");
printf("| |\n");
printf("| 1. Free memory of ptrA |\n");
printf("| 2. Free memory of ptrB |\n");
printf("| 3. Free memory of ptrC |\n");
printf("|_______________________________|\n");
l3: printf("\nEnter your choice -> ");
scanf("%d",&choice);
switch (choice)
{
case 1:
memFree(&ptrA);
break;
case 2:
memFree(&ptrB);
break;
case 3:
memFree(&ptrC);
break;
default:
printf("Please enter correct choice\n");
goto l3;
}
}
//function allocate memory
void memAlloc(int **ptr)
{
char str[100];
unsigned memblock = 0;
if(*ptr!=NULL)
{
printf("Already allocated\n");
return;
}
printf("Enter the number of memory block you want's to allocate\n");
scanf("%d",&memblock);
*ptr = (int*) malloc(memblock * sizeof(int)); //memory allocated using malloc
if(*ptr== NULL)
{
printf("Error! memory not allocated.");
return;
}
if(ptr == &ptrA)
{
memA = memblock*sizeof(int);
printf("Memory allocated to ptrA -> %d Byte\n",memA);
}
else if(ptr == &ptrB)
{
memB = memblock*sizeof(int);
printf("Memory allocated to ptrB -> %d Byte\n",memB);
}
else if(ptr == &ptrC)
{
memC = memblock*sizeof(int);
printf("Memory allocated to ptrC -> %d Byte\n",memC);
}
memory_alloc = memory_alloc + memblock*sizeof(int);
//log file handling for allocated memory
sprintf(str," %d\t\t\t%d\t\t\tU\n",*ptr,memblock*(sizeof(int)));
fp = fopen(FILENAME,"a");
fputs(str,fp);
fclose(fp);
}
//function deallocate memory
void memFree(int **ptr)
{
char str[100];
if(*ptr == NULL)
{
printf("Memory already free\n");
return;
}
if(ptr == &ptrA)
{
printf("Memory freed of ptrA -> %d Byte\n",memA);
sprintf(str," %d\t\t\t%d\t\t\tF\n",*ptr,memA);
memory_alloc -= memA;
}
else if(ptr == &ptrB)
{
printf("Memory freed of ptrB -> %d Byte\n",memB);
sprintf(str," %d\t\t\t%d\t\t\tF\n",*ptr,memB);
memory_alloc -= memB;
}
else if(ptr == &ptrC)
{
printf("Memory freed of ptrC -> %d Byte\n",memC);
sprintf(str," %d\t\t\t%d\t\t\tF\n",*ptr,memC);
memory_alloc -= memC;
}
free(*ptr);
*ptr = NULL;
//log file handling for deallocated memory
fp = fopen(FILENAME,"a");
fputs(str,fp);
fclose(fp);
}