OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
allocate2d.c
Go to the documentation of this file.
1 
7 #include "allocate2d.h"
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 
13 char **allocate2d_char(size_t h, size_t w){
14  char **p = (char**) malloc(h * sizeof(char*));
15  if (p == NULL) {
16  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
17  return NULL;
18  }
19  p[0] = (char*) malloc(w * h * sizeof(char));
20  if (p[0] == NULL) {
21  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
22  free(p);
23  return NULL;
24  }
25  for (size_t i = 1; i < h; i++) {
26  p[i] = &(p[0][i * w]);
27  }
28  return p;
29 }
30 void free2d_char(char **p) {
31  free(p[0]);
32  free(p);
33 }
34 void free2d_all_char(char **p, size_t length) {
35  for (size_t i = 0; i < length; i++) {
36  free(p[i]);
37  }
38  free(p);
39 }
40 
41 unsigned char **allocate2d_uchar(size_t h, size_t w){
42  unsigned char **p = (unsigned char**) malloc(h * sizeof(unsigned char*));
43  if (p == NULL) {
44  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
45  return NULL;
46  }
47  p[0] = (unsigned char*) malloc(w * h * sizeof(unsigned char));
48  if (p[0] == NULL) {
49  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
50  free(p);
51  return NULL;
52  }
53  for (size_t i = 1; i < h; i++) {
54  p[i] = &(p[0][i * w]);
55  }
56  return p;
57 }
58 void free2d_uchar(unsigned char **p) {
59  free(p[0]);
60  free(p);
61 }
62 void free2d_all_uchar(unsigned char **p, size_t length) {
63  for (size_t i = 0; i < length; i++) {
64  free(p[i]);
65  }
66  free(p);
67 }
68 
69 signed char **allocate2d_schar(size_t h, size_t w){
70  signed char **p = (signed char**) malloc(h * sizeof(signed char*));
71  if (p == NULL) {
72  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
73  return NULL;
74  }
75  p[0] = (signed char*) malloc(w * h * sizeof(signed char));
76  if (p[0] == NULL) {
77  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
78  free(p);
79  return NULL;
80  }
81  for (size_t i = 1; i < h; i++) {
82  p[i] = &(p[0][i * w]);
83  }
84  return p;
85 }
86 void free2d_schar(signed char **p) {
87  free(p[0]);
88  free(p);
89 }
90 void free2d_all_schar(signed char **p, size_t length) {
91  for (size_t i = 0; i < length; i++) {
92  free(p[i]);
93  }
94  free(p);
95 }
96 
97 short **allocate2d_short(size_t h, size_t w){
98  short **p = (short**) malloc(h * sizeof(short*));
99  if (p == NULL) {
100  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
101  return NULL;
102  }
103  p[0] = (short*) malloc(w * h * sizeof(short));
104  if (p[0] == NULL) {
105  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
106  free(p);
107  return NULL;
108  }
109  for (size_t i = 1; i < h; i++) {
110  p[i] = &(p[0][i * w]);
111  }
112  return p;
113 }
114 void free2d_short(short **p) {
115  free(p[0]);
116  free(p);
117 }
118 void free2d_all_short(short **p, size_t length) {
119  for (size_t i = 0; i < length; i++) {
120  free(p[i]);
121  }
122  free(p);
123 }
124 
125 float **allocate2d_float(size_t h, size_t w){
126  float **p = (float**) malloc(h * sizeof(float*));
127  if (p == NULL) {
128  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
129  return NULL;
130  }
131  p[0] = (float*) malloc(w * h * sizeof(float));
132  if (p[0] == NULL) {
133  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
134  free(p);
135  return NULL;
136  }
137  for (size_t i = 1; i < h; i++) {
138  p[i] = &(p[0][i * w]);
139  }
140  return p;
141 }
142 void free2d_float(float **p) {
143  free(p[0]);
144  free(p);
145 }
146 void free2d_all_float(float **p, size_t length) {
147  for (size_t i = 0; i < length; i++) {
148  free(p[i]);
149  }
150  free(p);
151 }
152 
153 double **allocate2d_double(size_t h, size_t w){
154  double **p = (double**) malloc(h * sizeof(double*));
155  if (p == NULL) {
156  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
157  return NULL;
158  }
159  p[0] = (double*) malloc(w * h * sizeof(double));
160  if (p[0] == NULL) {
161  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
162  free(p);
163  return NULL;
164  }
165  for (size_t i = 1; i < h; i++) {
166  p[i] = &(p[0][i * w]);
167  }
168  return p;
169 }
170 void free2d_double(double **p) {
171  free(p[0]);
172  free(p);
173 }
174 void free2d_all_double(double **p, size_t length) {
175  for (size_t i = 0; i < length; i++) {
176  free(p[i]);
177  }
178  free(p);
179 }
180 
void free2d_double(double **p)
Free a two-dimensional array created by allocate2d_double.
Definition: allocate2d.c:170
void free2d_schar(signed char **p)
Free a two-dimensional array created by allocate2d_schar.
Definition: allocate2d.c:86
#define NULL
Definition: decode_rs.h:63
float h[MODELMAX]
Definition: atrem_corl1.h:131
void free2d_all_double(double **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:174
void free2d_short(short **p)
Free a two-dimensional array created by allocate2d_short.
Definition: allocate2d.c:114
void free2d_uchar(unsigned char **p)
Free a two-dimensional array created by allocate2d_uchar.
Definition: allocate2d.c:58
void free2d_all_short(short **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:118
signed char ** allocate2d_schar(size_t h, size_t w)
Allocate a two-dimensional array of type signed char of a given size.
Definition: allocate2d.c:69
void free2d_float(float **p)
Free a two-dimensional array created by allocate2d_float.
Definition: allocate2d.c:142
void free2d_char(char **p)
Free a two-dimensional array created by allocate2d_char.
Definition: allocate2d.c:30
unsigned char ** allocate2d_uchar(size_t h, size_t w)
Allocate a two-dimensional array of type unsigned char of a given size.
Definition: allocate2d.c:41
Utility functions for allocating and freeing two-dimensional arrays of various types.
void free2d_all_uchar(unsigned char **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:62
double ** allocate2d_double(size_t h, size_t w)
Allocate a two-dimensional array of type double of a given size.
Definition: allocate2d.c:153
short ** allocate2d_short(size_t h, size_t w)
Allocate a two-dimensional array of type short of a given size.
Definition: allocate2d.c:97
float ** allocate2d_float(size_t h, size_t w)
Allocate a two-dimensional array of type float of a given size.
Definition: allocate2d.c:125
void free2d_all_schar(signed char **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:90
void free2d_all_char(char **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:34
int i
Definition: decode_rs.h:71
float p[MODELMAX]
Definition: atrem_corl1.h:131
char ** allocate2d_char(size_t h, size_t w)
Allocate a two-dimensional array of type char of a given size.
Definition: allocate2d.c:13
void free2d_all_float(float **p, size_t length)
Free a two-dimensional array created by malloc'ing each row individually.
Definition: allocate2d.c:146