Back to index

glibc  2.9
Defines | Functions
tst-calloc.c File Reference
#include <errno.h>
#include <error.h>
#include <limits.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>

Go to the source code of this file.

Defines

#define N   50000

Functions

static void fixed_test (int size)
static void random_test (void)
static void null_test (void)
int main (void)

Define Documentation

#define N   50000

Definition at line 29 of file tst-calloc.c.


Function Documentation

static void fixed_test ( int  size) [static]

Definition at line 33 of file tst-calloc.c.

{
  char *ptrs[N];
  int i;

  for (i = 0; i < N; ++i)
    {
      int j;

      ptrs[i] = (char *) calloc (1, size);

      if (ptrs[i] == NULL)
       break;

      for (j = 0; j < size; ++j)
       {
         if (ptrs[i][j] != '\0')
           error (EXIT_FAILURE, 0,
                 "byte not cleared (size %d, element %d, byte %d)",
                 size, i, j);
         ptrs[i][j] = '\xff';
       }
    }

  while (i-- > 0)
    free (ptrs[i]);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( void  )

Definition at line 109 of file tst-calloc.c.

{
  /* We are allocating blocks with `calloc' and check whether every
     block is completely cleared.  We first try this for some fixed
     times and then with random size.  */
  fixed_test (15);
  fixed_test (5);
  fixed_test (17);
  fixed_test (6);
  fixed_test (31);
  fixed_test (96);

  random_test ();

  null_test ();

  return 0;
}

Here is the call graph for this function:

static void null_test ( void  ) [static]

Definition at line 96 of file tst-calloc.c.

{
  /* If the size is 0 the result is implementation defined.  Just make
     sure the program doesn't crash.  */
  calloc (0, 0);
  calloc (0, UINT_MAX);
  calloc (UINT_MAX, 0);
  calloc (0, ~((size_t) 0));
  calloc (~((size_t) 0), 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void random_test ( void  ) [static]

Definition at line 63 of file tst-calloc.c.

{
  char *ptrs[N];
  int i;

  for (i = 0; i < N; ++i)
    {
      int j;
      int n = 1 + random () % 10;
      int elem = 1 + random () % 100;
      int size = n * elem;

      ptrs[i] = (char *) calloc (n, elem);

      if (ptrs[i] == NULL)
       break;

      for (j = 0; j < size; ++j)
       {
         if (ptrs[i][j] != '\0')
           error (EXIT_FAILURE, 0,
                 "byte not cleared (size %d, element %d, byte %d)",
                 size, i, j);
         ptrs[i][j] = '\xff';
       }
    }

  while (i-- > 0)
    free (ptrs[i]);
}

Here is the call graph for this function:

Here is the caller graph for this function: