Back to index

glibc  2.9
tst-malloc.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <malloc.h>
00022 #include <stdio.h>
00023 
00024 static int errors = 0;
00025 
00026 static void
00027 merror (const char *msg)
00028 {
00029   ++errors;
00030   printf ("Error: %s\n", msg);
00031 }
00032 
00033 int
00034 main (void)
00035 {
00036   void *p, *q;
00037   int save;
00038 
00039   errno = 0;
00040 
00041   p = malloc (-1);
00042   save = errno;
00043 
00044   if (p != NULL)
00045     merror ("malloc (-1) succeeded.");
00046 
00047   if (p == NULL && save != ENOMEM)
00048     merror ("errno is not set correctly");
00049 
00050   p = malloc (10);
00051   if (p == NULL)
00052     merror ("malloc (10) failed.");
00053 
00054   /* realloc (p, 0) == free (p).  */
00055   p = realloc (p, 0);
00056   if (p != NULL)
00057     merror ("realloc (p, 0) failed.");
00058 
00059   p = malloc (0);
00060   if (p == NULL)
00061     merror ("malloc (0) failed.");
00062 
00063   p = realloc (p, 0);
00064   if (p != NULL)
00065     merror ("realloc (p, 0) failed.");
00066 
00067   p = malloc (513 * 1024);
00068   if (p == NULL)
00069     merror ("malloc (513K) failed.");
00070 
00071   q = malloc (-512 * 1024);
00072   if (q != NULL)
00073     merror ("malloc (-512K) succeeded.");
00074 
00075   free (p);
00076 
00077   return errors != 0;
00078 }