This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
150 lines (118 loc) · 3.8 KB
/
main.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
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
// C11 standard
// created by cicek on Feb 13, 2020 09:18
#include <stdio.h>
#include <stdlib.h>
#include "new_malloc.h"
int main() {
/* -------------------------- Normal malloc() ------------------------- */
printf("\nNormal malloc():\n");
int *ptr;
/* normal malloc() fonksiyonu - a block of 10 ints */
/* malloc() returns a pointer from the allocated memory */
ptr = malloc(10 * sizeof(int));
if (ptr != NULL) {
*(ptr + 3) = 49; /* index 3'e 49'u ata - assign 49 to 3rd int */
}
/* print -> index - value - address */
printf("index\tvalue\taddress\n");
for (int i = 0; i < 10; i++) {
printf("%d\t->%3d\t -> %p\n", i, *(ptr + i), (ptr + i));
}
/* normal free() */
free(ptr);
/* --------------------------- new_malloc() --------------------------- */
printf("\nnew_malloc():\n");
int *new_ptr;
/* yeni yazdığımız new_malloc() fonksiyonu - a block of 10 ints */
/* new_malloc() returns a pointer from the allocated memory */
new_ptr = new_malloc(10 * sizeof(int));
if (new_ptr != NULL) {
*(new_ptr + 7) = 99; /* index'lere atama işlemleri */
*(new_ptr + 0) = 12;
*(new_ptr + 2) = 33;
*(new_ptr + 3) = -45;
*(new_ptr + 5) = 77;
*(new_ptr + 8) = 11;
}
/* print -> index - value - address */
printf("index\tvalue\taddress\n");
for (int i = 0; i < 10; i++) {
printf("%d\t->%3d\t -> %p\n", i, *(new_ptr + i), (new_ptr + i));
}
/* new_free() */
new_free(new_ptr);
printf("\n***\n"
"Alan tahsisi ve serbest bırakılması başarıyla tamamlandı!"
"\nAllocation and deallocation is done successfully!\n"
"***\n");
/* OUTPUTS / ÇIKTILAR (IDE - Terminal):
*
*
running from IDE:
Normal malloc():
index value address
0 -> 0 -> 0x5597d4aab6b0
1 -> 0 -> 0x5597d4aab6b4
2 -> 0 -> 0x5597d4aab6b8
3 -> 49 -> 0x5597d4aab6bc
4 -> 0 -> 0x5597d4aab6c0
5 -> 0 -> 0x5597d4aab6c4
6 -> 0 -> 0x5597d4aab6c8
7 -> 0 -> 0x5597d4aab6cc
8 -> 0 -> 0x5597d4aab6d0
9 -> 0 -> 0x5597d4aab6d4
new_malloc():
index value address
0 -> 12 -> 0x5597d4acffd0
1 -> 0 -> 0x5597d4acffd4
2 -> 33 -> 0x5597d4acffd8
3 ->-45 -> 0x5597d4acffdc
4 -> 0 -> 0x5597d4acffe0
5 -> 77 -> 0x5597d4acffe4
6 -> 0 -> 0x5597d4acffe8
7 -> 99 -> 0x5597d4acffec
8 -> 11 -> 0x5597d4acfff0
9 -> 0 -> 0x5597d4acfff4
***
Alan tahsisi ve serbest bırakılması başarıyla tamamlandı!
Allocation and deallocation is done successfully!
***
Process finished with exit code 0
*/
/*
*
*
running from terminal:
cicek@ubuntu2004:~$ gcc main.c new_malloc.c
cicek@ubuntu2004:~$ ./a.out
Normal malloc():
index value address
0 -> 0 -> 0x55d1d99f86b0
1 -> 0 -> 0x55d1d99f86b4
2 -> 0 -> 0x55d1d99f86b8
3 -> 49 -> 0x55d1d99f86bc
4 -> 0 -> 0x55d1d99f86c0
5 -> 0 -> 0x55d1d99f86c4
6 -> 0 -> 0x55d1d99f86c8
7 -> 0 -> 0x55d1d99f86cc
8 -> 0 -> 0x55d1d99f86d0
9 -> 0 -> 0x55d1d99f86d4
new_malloc():
index value address
0 -> 12 -> 0x55d1d9a1cfd0
1 -> 0 -> 0x55d1d9a1cfd4
2 -> 33 -> 0x55d1d9a1cfd8
3 ->-45 -> 0x55d1d9a1cfdc
4 -> 0 -> 0x55d1d9a1cfe0
5 -> 77 -> 0x55d1d9a1cfe4
6 -> 0 -> 0x55d1d9a1cfe8
7 -> 99 -> 0x55d1d9a1cfec
8 -> 11 -> 0x55d1d9a1cff0
9 -> 0 -> 0x55d1d9a1cff4
***
Alan tahsisi ve serbest bırakılması başarıyla tamamlandı!
Allocation and deallocation is done successfully!
***
*/
return 0;
}