-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.c
More file actions
executable file
·60 lines (45 loc) · 1.05 KB
/
test3.c
File metadata and controls
executable file
·60 lines (45 loc) · 1.05 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
/* Stuart Norcross - 12/03/10 */
/* This program allocates integer arrays and displays trace information*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "myalloc.h"
#define INTS_PER_ALLOCATION 10
int alloc_size=sizeof(int)*INTS_PER_ALLOCATION;
void check_failed(int val){
fprintf(stderr, "Check failed for region with value %i.",val);
exit(-1);
}
void check(int *mem, int value){
int i;
for(i=0;i<INTS_PER_ALLOCATION;i++){
if(mem[i]!=value)check_failed(value);
}
}
void set(int *mem, int value){
int i;
for(i=0;i<INTS_PER_ALLOCATION;i++){
mem[i]=value;
}
}
int *alloc_and_set(int value){
int* mem = (int*)myalloc(alloc_size);
set(mem,value);
return mem;
}
int main(int argc, char* argv[]){
printf("%s starting\n",argv[0]);
// allocate
void *p1 = alloc_and_set(1);
printf("TEST 1 PASSED - ALLOCATED\n");
check(p1,1);
//allocate
void *p2 = alloc_and_set(2);
check(p2,2);
//free
myfree(p1);
printf("TEST 2 PASSED - FREED\n");
myfree(p2);
printf("TEST 3 PASSED - FREED\n");
printf("%s complete\n",argv[0]);
}