File: testalloc0.cpp

package info (click to toggle)
duma 2.5.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,884 kB
  • sloc: ansic: 5,308; cpp: 2,204; makefile: 695; sh: 90
file content (95 lines) | stat: -rw-r--r-- 1,800 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <stdlib.h>
#include <stdio.h>
#include <new>

int get_new0strategy(void)
{
  /* test for behaviour of operator new with size 0 */
  int op_new_0_strategy;
  int * piNullPtrA;
  int * piNullPtrB;

  piNullPtrA = (int*) operator new(0);
  piNullPtrB = (int*) operator new(0);

  if ( !piNullPtrA )
    op_new_0_strategy = 1;
  else if ( piNullPtrA == piNullPtrB )
    op_new_0_strategy = 2;
  else
    op_new_0_strategy = 3;

  delete piNullPtrA;
  delete piNullPtrB;

  return op_new_0_strategy;
}

int get_newVec0strategy(void)
{
  /* test for behaviour of operator new with size 0 */
  int op_new_0_strategy;
  int * piNullPtrA;
  int * piNullPtrB;

  piNullPtrA = new int[0];
  piNullPtrB = new int[0];

  if ( !piNullPtrA )
    op_new_0_strategy = 1;
  else if ( piNullPtrA == piNullPtrB )
    op_new_0_strategy = 2;
  else
    op_new_0_strategy = 3;

  delete []piNullPtrA;
  delete []piNullPtrB;

  return op_new_0_strategy;
}

int get_malloc0strategy(void)
{
  int op_new_0_strategy;
  int * piNullPtrA;
  int * piNullPtrB;

  piNullPtrA = (int*)malloc(0);
  piNullPtrB = (int*)malloc(0);

  if ( !piNullPtrA )
    op_new_0_strategy = 1;
  else if ( piNullPtrA == piNullPtrB )
    op_new_0_strategy = 2;
  else
    op_new_0_strategy = 3;

  free(piNullPtrA);
  free(piNullPtrB);

  return op_new_0_strategy;
}


int main(int argc, char *argv[])
{
  int test = 0;
  int strategy;

  if ( argc >= 1 )
    test = atoi( argv[1] );

  switch (test)
  {
    default:
    case 0:  printf("testing malloc(0) .. "); strategy = get_malloc0strategy();  break;
    case 1:  printf("testing new(0) .. ");    strategy = get_new0strategy();     break;
    case 2:  printf("testing new[0] .. ");    strategy = get_newVec0strategy();  break;
  }

  printf("result is %d\n", strategy);

  return 0;
}