-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
46 lines (37 loc) · 1.04 KB
/
main.cpp
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
#include <iostream>
#include "memalloc.h"
int main() {
// Allocate memory using custom malloc
int* arr = (int*) malloc(5 * sizeof(int));
if (arr == nullptr) {
std::cerr << "Memory allocation failed" << std::endl;
return 1;
}
for (int i = 0; i < 5; ++i) {
arr[i] = i * 10;
}
std::cout << "Array elements: ";
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// Reallocate memory using custom realloc
arr = (int*) realloc(arr, 10 * sizeof(int));
if (arr == nullptr) {
std::cerr << "Memory reallocation failed" << std::endl;
return 1;
}
for (int i = 5; i < 10; ++i) {
arr[i] = i * 10;
}
std::cout << "Array elements after reallocation: ";
for (int i = 0; i < 10; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// Free the allocated memory using custom free
free(arr);
// Print the memory list for debugging purposes
print_mem_list();
return 0;
}