-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_prac1.c
52 lines (41 loc) · 1.15 KB
/
heap_prac1.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
#include <stdio.h>
#include <stdlib.h>
/* This program changes heap values. On the system it
* was tested on, it was only able to change values up
* to 2 ints worth of memory space above the malloc'ed
* memory space. On the other hand, descending memory
* addresses were able to be modified and printed up
* to 168 ints worth of data below the malloc'ed addy.
*/
int main() {
int *i = malloc(sizeof(int) * 3);
*(i) = 50;
*(i + 1) = 51;
*(i + 2) = 52;
*(i + 3) = 53;
*(i + 4) = 54;
*(i + 5) = 55;
int count = 1;
for (int g=0; g<7; g++) {
if (*(i + g) == 0) {
*(i + g) = count;
count += 1;
}
}
printf("\n");
count = 1;
for (int j=0; j<20; j++) {
printf("%p || i + %03d || dec: %.10d || hex: 0x%08x\n", i+j, j, *(i+j), *(i+j));
}
printf("\nGoing downwards...\n\n");
count = 10;
for (int g=0; g>-169; g--) {
*(i+g) = count;
count += 1;
}
for (int j=0; j>-169; j--) {
printf("%p || i - %03d || dec: %.10d || hex: 0x%08x\n", i-j, -j, *(i+j), *(i+j));
}
printf("\n");
return 0;
}