OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
allocate3d.c
Go to the documentation of this file.
1 
7 #include "allocate3d.h"
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 
13 short ***allocate3d_short(size_t nz, size_t ny, size_t nx){
14  short *x_ptr = (short*) malloc(nz * ny * nx * sizeof(short));
15  if (x_ptr == NULL) {
16  fprintf(stderr, "-E- %s line %d: Memory allocation of data block failed.n", __FILE__, __LINE__);
17  return NULL;
18  }
19  short **y_ptr = (short**) malloc(nz * ny * sizeof(short*));
20  if (y_ptr == NULL) {
21  fprintf(stderr, "-E- %s line %d: Memory allocation of y array failed.n", __FILE__, __LINE__);
22  return NULL;
23  }
24  short ***z_ptr = (short***) malloc(nz * sizeof(short**));
25  if (z_ptr == NULL) {
26  fprintf(stderr, "-E- %s line %d: Memory allocation of z array failed.n", __FILE__, __LINE__);
27  return NULL;
28  }
29  for(size_t z=0; z<nz; z++) {
30  for(size_t y=0; y<ny; y++) {
31  y_ptr[y] = x_ptr;
32  x_ptr += nx;
33  }
34  z_ptr[z] = y_ptr;
35  y_ptr += ny;
36  }
37  return z_ptr;
38 }
39 void free3d_short(short ***p) {
40  free(p[0][0]);
41  free(p[0]);
42  free(p);
43 }
44 
45 int ***allocate3d_int(size_t nz, size_t ny, size_t nx){
46  int *x_ptr = (int*) malloc(nz * ny * nx * sizeof(int));
47  if (x_ptr == NULL) {
48  fprintf(stderr, "-E- %s line %d: Memory allocation of data block failed.n", __FILE__, __LINE__);
49  return NULL;
50  }
51  int **y_ptr = (int**) malloc(nz * ny * sizeof(int*));
52  if (y_ptr == NULL) {
53  fprintf(stderr, "-E- %s line %d: Memory allocation of y array failed.n", __FILE__, __LINE__);
54  return NULL;
55  }
56  int ***z_ptr = (int***) malloc(nz * sizeof(int**));
57  if (z_ptr == NULL) {
58  fprintf(stderr, "-E- %s line %d: Memory allocation of z array failed.n", __FILE__, __LINE__);
59  return NULL;
60  }
61  for(size_t z=0; z<nz; z++) {
62  for(size_t y=0; y<ny; y++) {
63  y_ptr[y] = x_ptr;
64  x_ptr += nx;
65  }
66  z_ptr[z] = y_ptr;
67  y_ptr += ny;
68  }
69  return z_ptr;
70 }
71 void free3d_int(int ***p) {
72  free(p[0][0]);
73  free(p[0]);
74  free(p);
75 }
76 
77 float ***allocate3d_float(size_t nz, size_t ny, size_t nx){
78  float *x_ptr = (float*) malloc(nz * ny * nx * sizeof(float));
79  if (x_ptr == NULL) {
80  fprintf(stderr, "-E- %s line %d: Memory allocation of data block failed.n", __FILE__, __LINE__);
81  return NULL;
82  }
83  float **y_ptr = (float**) malloc(nz * ny * sizeof(float*));
84  if (y_ptr == NULL) {
85  fprintf(stderr, "-E- %s line %d: Memory allocation of y array failed.n", __FILE__, __LINE__);
86  return NULL;
87  }
88  float ***z_ptr = (float***) malloc(nz * sizeof(float**));
89  if (z_ptr == NULL) {
90  fprintf(stderr, "-E- %s line %d: Memory allocation of z array failed.n", __FILE__, __LINE__);
91  return NULL;
92  }
93  for(size_t z=0; z<nz; z++) {
94  for(size_t y=0; y<ny; y++) {
95  y_ptr[y] = x_ptr;
96  x_ptr += nx;
97  }
98  z_ptr[z] = y_ptr;
99  y_ptr += ny;
100  }
101  return z_ptr;
102 }
103 void free3d_float(float ***p) {
104  free(p[0][0]);
105  free(p[0]);
106  free(p);
107 }
108 
109 double ***allocate3d_double(size_t nz, size_t ny, size_t nx){
110  double *x_ptr = (double*) malloc(nz * ny * nx * sizeof(double));
111  if (x_ptr == NULL) {
112  fprintf(stderr, "-E- %s line %d: Memory allocation of data block failed.n", __FILE__, __LINE__);
113  return NULL;
114  }
115  double **y_ptr = (double**) malloc(nz * ny * sizeof(double*));
116  if (y_ptr == NULL) {
117  fprintf(stderr, "-E- %s line %d: Memory allocation of y array failed.n", __FILE__, __LINE__);
118  return NULL;
119  }
120  double ***z_ptr = (double***) malloc(nz * sizeof(double**));
121  if (z_ptr == NULL) {
122  fprintf(stderr, "-E- %s line %d: Memory allocation of z array failed.n", __FILE__, __LINE__);
123  return NULL;
124  }
125  for(size_t z=0; z<nz; z++) {
126  for(size_t y=0; y<ny; y++) {
127  y_ptr[y] = x_ptr;
128  x_ptr += nx;
129  }
130  z_ptr[z] = y_ptr;
131  y_ptr += ny;
132  }
133  return z_ptr;
134 }
135 void free3d_double(double ***p) {
136  free(p[0][0]);
137  free(p[0]);
138  free(p);
139 }
140 
Utility functions for allocating and freeing three-dimensional arrays of various types.
void free3d_int(int ***p)
Free a three-dimensional array created by allocate3d_int.
Definition: allocate3d.c:71
int *** allocate3d_int(size_t nz, size_t ny, size_t nx)
Allocate a three-dimensional array of type int of a given size.
Definition: allocate3d.c:45
#define NULL
Definition: decode_rs.h:63
short *** allocate3d_short(size_t nz, size_t ny, size_t nx)
Allocate a three-dimensional array of type short of a given size.
Definition: allocate3d.c:13
void free3d_short(short ***p)
Free a three-dimensional array created by allocate3d_short.
Definition: allocate3d.c:39
float *** allocate3d_float(size_t nz, size_t ny, size_t nx)
Allocate a three-dimensional array of type float of a given size.
Definition: allocate3d.c:77
void free3d_float(float ***p)
Free a three-dimensional array created by allocate3d_float.
Definition: allocate3d.c:103
void free3d_double(double ***p)
Free a three-dimensional array created by allocate3d_double.
Definition: allocate3d.c:135
double *** allocate3d_double(size_t nz, size_t ny, size_t nx)
Allocate a three-dimensional array of type double of a given size.
Definition: allocate3d.c:109
float p[MODELMAX]
Definition: atrem_corl1.h:131