-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorchecked_heap.c
58 lines (45 loc) · 1.88 KB
/
errorchecked_heap.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *errorchecked_malloc(long int); // function prototype for errorchecked_malloc()
int main(int argc, char *argv[]) {
char esc = 'X'; // exit point
char *char_ptr; // a char pointer
int *int_ptr; // an integer pointer
long int mem_size;
if (argc < 2) // if there aren't commandline arguments,
mem_size = 50; // use 50 as the default value..
else
mem_size = atol(argv[1]);
printf("\t[+] allocating %d bytes of memory on the heap for char_ptr\n", mem_size);
char_ptr = (char *) errorchecked_malloc(mem_size); // allocating heap memory
strcpy(char_ptr, "This is memory is located on the heap.");
printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
printf("\t[+] allocating 12 bytes of memory on the heap for int_ptr\n");
int_ptr = (int *) errorchecked_malloc(12); // allocated heap memory again
*int_ptr = 31337; // put the value of 31337 where int_ptr is pointing
printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);
printf("\t[-] freeing char_ptr's heap memory...\n");
free(char_ptr); // freeing heap memory
printf("\t[+] allocating another 15 bytes for char_ptr\n");
char_ptr = (char *) errorchecked_malloc(15); // allocating more heap memory
strcpy(char_ptr, "new memory");
printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);
printf("\t[-] freeing int_ptr's heap memory...\n");
free(int_ptr); // freeing heap memory
printf("\t[-] freeing char_ptr's heap memory...\n");
free(char_ptr); // freeing the other block of heap memory
// exit point
puts("\nexit point:");
scanf(" %c", &esc);
return 0;
}
void *errorchecked_malloc(long int size) { // an error checked malloc() function
void *ptr;
ptr = malloc(size);
if(ptr == 0) {
fprintf(stderr, "Error: could not allocate heap memory.\n");
exit(-1);
}
return ptr;
}