Project

General

Profile

test_malloc.c

ddegroot, 12/31/2017 04:14 AM

 
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <errno.h>
5

    
6
// On Linux
7
#if !defined(SIZE_MAX)
8
#define SIZE_MAX __SIZE_MAX__                // ((size_t)-1) = 9223372036854775807 = LONG_MAX
9
#endif
10
// On DragonFlyBSD
11
//#define SIZE_MAX UINT64_MAX  // from /usr/include/machine/int_limits.h
12

    
13
int main () {
14
  printf("SIZE_MAX=%lu\n", SIZE_MAX);
15
  
16
  unsigned long tests[] = {
17
    9223372036854775807UL,
18
    223372036854775808UL,
19
    23372036854775808UL,
20
    3372036854775808UL,
21
    372036854775808UL,
22
    0x7ff7bf77f001UL,
23
    0x7ff7bf77f000UL,
24
    72036854775808UL,
25
    2036854775808UL,
26
    36854775808UL,
27
    INTPTR_MAX,
28
    UINTPTR_MAX,
29
    SIZE_MAX - 2,
30
    SIZE_MAX,
31
    SIZE_MAX + 'a',
32
    SIZE_MAX + SIZE_MAX,
33
    SIZE_MAX * 20
34
  };
35
  
36
  for (int test = 0; test < 16; test++) {
37
//  for (int test = 0; test < 2; test++) {
38
    printf("test%d: malloc size:%lu, ", test, tests[test]);
39

    
40
    uint *ptr = (int *) malloc(tests[test]);
41
    if (ptr == NULL) {
42
        printf("malloc failed, ptr == NULL, errno:%d\n", errno);
43
    } else {
44
        printf(" ptr == %p\n", ptr);
45
        free(ptr);
46
        ptr = NULL;
47
    }
48
  }
49
  return 0;
50
}