File: malloc2.c

package info (click to toggle)
valgrind 1%3A3.2.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,372 kB
  • ctags: 23,091
  • sloc: ansic: 192,648; xml: 10,723; sh: 4,750; perl: 4,023; makefile: 2,103; asm: 1,813; cpp: 140; haskell: 139
file content (49 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download | duplicates (7)
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

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

/* The original test driver machinery. */
#define N_TEST_TRANSACTIONS 500
#define N_TEST_ARR 2000

#define M_TEST_MALLOC 1000

void* test_arr[N_TEST_ARR];

int main ( int argc, char** argv )
{
   int i, j, k, nbytes;
   unsigned char* chp;

   for (i = 0; i < N_TEST_ARR; i++)
      test_arr[i] = NULL;

   for (i = 0; i < N_TEST_TRANSACTIONS; i++) {
      j = random() % N_TEST_ARR;
      if (test_arr[j]) {
         free(test_arr[j]);
         test_arr[j] = NULL;
      } else {
         nbytes = 1 + random() % M_TEST_MALLOC;
         if (random()%64 == 32) 
            nbytes *= 17;
         test_arr[j] = malloc( nbytes );
         chp = test_arr[j];
         for (k = 1; k < nbytes; k++) 
            chp[k] = (unsigned char)(k + 99);
      }
   }

   for (i = 0; test_arr[i] == NULL; i++) ;
   free(test_arr[i]);
   ((char*)test_arr[i])[0] = 0;

   for (i = 0; i < N_TEST_ARR; i++) {
      if (test_arr[i]) {
         free(test_arr[i]);
         test_arr[i] = NULL;
      }
   }

   return 0;
}