-
Notifications
You must be signed in to change notification settings - Fork 1
/
safeAlloc.c
47 lines (39 loc) · 860 Bytes
/
safeAlloc.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
#include "safeAlloc.h"
void *safeMalloc(int n) {
void *ptr = malloc(n);
if (ptr == NULL) {
perror("Allocation failed.\n");
exit(EXIT_FAILURE);
}
return ptr;
}
void *safeCalloc(int n, int size) {
void *ptr = calloc(n, size);
if (ptr == NULL) {
perror("Allocation failed.\n");
exit(EXIT_FAILURE);
}
return ptr;
}
int intParse(const char *arg) {
char *end;
long strParse;
strParse = strtol(arg, &end, 10);
errno = 0;
if (errno != 0 || *end != '\0' || strParse > INT_MAX) {
perror("Error while converting arg.\n");
exit(EXIT_FAILURE);
}
return (int)strParse;
}
float floatParse(const char *arg) {
char *end;
float strParse;
strParse = strtof(arg, &end);
errno = 0;
if (errno != 0 || *end != '\0') {
perror("Error while converting arg.\n");
exit(EXIT_FAILURE);
}
return strParse;
}