File: malloc2.c

package info (click to toggle)
valgrind 1%3A3.3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 34,452 kB
  • ctags: 27,778
  • sloc: ansic: 234,398; sh: 14,186; xml: 11,662; perl: 4,410; asm: 3,135; makefile: 3,011; exp: 625; cpp: 255; haskell: 195
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;
}