-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmallocme.c
More file actions
65 lines (53 loc) · 1.71 KB
/
mallocme.c
File metadata and controls
65 lines (53 loc) · 1.71 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
61
62
63
64
65
/************************************************************************/
/* mallocme.c */
/* Test program for implementation of malloc and free found in memlib.h */
/************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "memlib.h"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define malloc( x ) my_malloc( x, __FILENAME__, __LINE__ )
#define free( x ) my_free( x, 0, __FILENAME__, __LINE__ )
typedef struct mystruct {
int i1;
int i2;
} s;
int main(int argc, char **argv) {
if(argc != 3)
{
printf("ERROR: Expected input in form of 2 integers.\n");
return 1;
}
else
{
char * a = argv[1];
char * b = argv[2];
int curr1 = atoi(a);
int curr2 = atoi(b);
printf("Allocating memory for a new struct...\n");
s * mynew = (s *) malloc(sizeof(s));
if( mynew != NULL )
{
printf("Malloc succeeded, adding data...\n");
mynew->i1 = curr1;
mynew->i2 = curr2;
printf("Data added. Trying to access...\n");
printf("First datum: %i\n", mynew->i1);
printf("Second datum: %i\n", mynew->i2);
}
printf("Freeing struct...\n");
free(mynew);
printf("Now, let's test some errors...\n");
printf("First error test...\n");
free(mynew);
printf("Second error test...\n");
s * mynon;
free(mynon);
char c = 'd';
char * cptr = &c;
free(cptr);
printf("Exiting...\n");
return 0;
}
}