pihmRasterLIBS/callocate.cpp

Go to the documentation of this file.
00001 #ifndef NULL
00002 #define NULL    0
00003 #endif
00004 
00005 /*
00006  * Copyright (C) 1996 by Environmental Systems Research Institute Inc.
00007  * All Rights Reserved.
00008  *
00009  * N O T I C E
00010  *
00011  * THIS MATERIAL IS BEING PRIVIDED TO ARCVIEW GIS USERS BY  ESRI.
00012  * UNAUTHORIZED ACCESS IS PROHIBITED
00013  */
00014 
00015 
00016 /*
00017 *-------------------------------------------------------------------
00018 *
00019 *       CAllocate1 - Allocate a one dimensional array
00020 *
00021 *-------------------------------------------------------------------
00022 *P      PURPOSE:
00023 *
00024 *       CAllocate1 dynamically allocates a one dimensional array
00025 *
00026 *E
00027 *-------------------------------------------------------------------
00028 *A      ARGUMENTS:
00029 *
00030 *       number <Input> (int) number of elements in array
00031 *       size   <Input> (int) size in bytes of an array element
00032 *
00033 *       RETURN VALUE    <Output> (char *) Pointer to allocated memory
00034 *                                         NULL == allocate failed
00035 *
00036 *E
00037 *-------------------------------------------------------------------
00038 *H      HISTORY:
00039 *       
00040 *E
00041 *-------------------------------------------------------------------
00042 */
00043 #include        <stdlib.h>
00044 
00045 
00046 char *CAllocate1 (int   number ,
00047                     int size )
00048 
00049 {
00050         return (char *)calloc(number, size);
00051 }
00052 
00053         
00054 /*
00055 *-------------------------------------------------------------------
00056 *
00057 *       CAllocate2 - Allocate a two dimensional array
00058 *
00059 *-------------------------------------------------------------------
00060 *P      PURPOSE:
00061 *
00062 *       CAllocate2 dynamically allocates a two dimensional array
00063 *
00064 *E
00065 *-------------------------------------------------------------------
00066 *A      ARGUMENTS:
00067 *
00068 *       nrows  <Input> (int) number of rows in array
00069 *       ncols  <Input> (int) number of cols in array
00070 *       size   <Input> (int) size in bytes of an array element
00071 *
00072 *       RETURN VALUE    <Output> (char **) Pointer to allocated memory
00073 *                                         NULL == allocate failed
00074 *
00075 *E
00076 *-------------------------------------------------------------------
00077 *H      HISTORY:
00078 *E
00079 *-------------------------------------------------------------------
00080 */
00081 
00082 char **CAllocate2 (int  nrows ,
00083                    int  ncols ,
00084                    int  size )
00085 
00086 {
00087 char    *data;
00088 char    **rows;
00089 char    **ptr;
00090 int     len;
00091 int     cur;
00092 int     i;
00093 
00094         if ((data = (char *)calloc (nrows*ncols, size)) == NULL) {
00095                 return (NULL);
00096         }
00097 
00098         if ((rows = (char **)calloc (nrows, sizeof(char **))) == NULL) {
00099                 free ((char *)data);
00100                 return (NULL);
00101         }
00102 
00103         ptr = rows;
00104         len = ncols*size;
00105         cur = 0;
00106 
00107         for (i=0; i<nrows; i++)
00108         {
00109                 *ptr++ = (char *) &(data[cur]);
00110                 cur += len;
00111         }
00112 
00113         return (char **)rows ;
00114 
00115         /*
00116         Note :
00117         -----------------------------------------------
00118         The GRIDIO libraries REQUIRE that
00119         the underlying data array for a two dimensional
00120         buffer is 1-Dimensional CONTIGUOS, as above
00121         -----------------------------------------------
00122         */
00123 }
00124 
00125 /*
00126 *-------------------------------------------------------------------
00127 *
00128 *       CFree1 - Free a one dimensional array
00129 *
00130 *-------------------------------------------------------------------
00131 *P      PURPOSE:
00132 *
00133 *       CFree1 frees a one dimensional array
00134 *
00135 *E
00136 *-------------------------------------------------------------------
00137 *A      ARGUMENTS:
00138 *
00139 *       ptr   <Input> (char *) pointer to space to be freed
00140 *
00141 *E
00142 *-------------------------------------------------------------------
00143 *H      HISTORY:
00144 *
00145 *E
00146 *-------------------------------------------------------------------
00147 */
00148 
00149 void CFree1 (char       *ptr)
00150 {
00151         free(ptr);
00152 }
00153 
00154 
00155 /*
00156 *-------------------------------------------------------------------
00157 *
00158 *       CFree2 - Frees a two dimensional array
00159 *
00160 *-------------------------------------------------------------------
00161 *P      PURPOSE:
00162 *
00163 *       CFree2 frees a two dimensional array
00164 *
00165 *E
00166 *-------------------------------------------------------------------
00167 *A      ARGUMENTS:
00168 *
00169 *       ptr    <Input> (char **) ptr to space to be freed
00170 *       nrows  <Input> (int    ) number of rows in array
00171 *
00172 *E
00173 *-------------------------------------------------------------------
00174 *H      HISTORY:
00175 *
00176 *E
00177 *-------------------------------------------------------------------
00178 */
00179 
00180 void CFree2 (char       **ptr,
00181         int     nrows)
00182 {
00183         char    *h;
00184         int     i;
00185 
00186         for (i = 1, h = ptr[0]; i < nrows; i++) {
00187                 if (h > ptr[i]) {
00188                         h = ptr[i];
00189                 }
00190         }
00191 
00192         free(h); free((char *)ptr);
00193 }
00194 

Generated on Sun Aug 5 17:34:00 2007 for PIHMgis by  doxygen 1.5.2