-
Notifications
You must be signed in to change notification settings - Fork 3
/
calloc_and_realloc.c
56 lines (48 loc) · 1.2 KB
/
calloc_and_realloc.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
/*
* Author: NagaChaitanya Vellanki
*
*
* calloc and realloc example
* gcc -Wall -pedantic -o calloc_and_realloc calloc_and_realloc.c
* ------------------------------------------------------------
* export MALLOC_TRACE=/tmp/t
* ------------------------------------------------------------
* ./calloc_and_realloc
* ------------------------------------------------------------
* mtrace ./calloc_and_realloc $MALLOC_TRACE
*
* Memory not freed:
* -----------------
* Address Size Caller
* 0x0000000000601460 0x50 at 0x40089b
*/
#include <errno.h>
#include <mcheck.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
void errExit(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
int *arr;
int *newPtr;
mtrace();
arr = (int *) calloc(10, sizeof(int));
if(arr == NULL) {
errExit("error on calloc, %s\n", strerror(errno));
}
newPtr = realloc(arr, 20 * sizeof(int));
if(newPtr == NULL) {
errExit("error on realloc, %s\n", strerror(errno));
} else {
arr = newPtr;
}
muntrace();
exit(EXIT_SUCCESS);
}