OB.DAAC Logo
NASA Logo
Ocean Color Science Software

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