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 
35 unsigned char **allocate2d_uchar(size_t h, size_t w){
36  unsigned char **p = (unsigned char**) malloc(h * sizeof(unsigned char*));
37  if (p == NULL) {
38  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
39  return NULL;
40  }
41  p[0] = (unsigned char*) malloc(w * h * sizeof(unsigned char));
42  if (p[0] == NULL) {
43  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
44  free(p);
45  return NULL;
46  }
47  for (size_t i = 1; i < h; i++) {
48  p[i] = &(p[0][i * w]);
49  }
50  return p;
51 }
52 void free2d_uchar(unsigned char **p) {
53  free(p[0]);
54  free(p);
55 }
56 
57 signed char **allocate2d_schar(size_t h, size_t w){
58  signed char **p = (signed char**) malloc(h * sizeof(signed char*));
59  if (p == NULL) {
60  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
61  return NULL;
62  }
63  p[0] = (signed char*) malloc(w * h * sizeof(signed char));
64  if (p[0] == NULL) {
65  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
66  free(p);
67  return NULL;
68  }
69  for (size_t i = 1; i < h; i++) {
70  p[i] = &(p[0][i * w]);
71  }
72  return p;
73 }
74 void free2d_schar(signed char **p) {
75  free(p[0]);
76  free(p);
77 }
78 
79 short **allocate2d_short(size_t h, size_t w){
80  short **p = (short**) malloc(h * sizeof(short*));
81  if (p == NULL) {
82  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
83  return NULL;
84  }
85  p[0] = (short*) malloc(w * h * sizeof(short));
86  if (p[0] == NULL) {
87  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
88  free(p);
89  return NULL;
90  }
91  for (size_t i = 1; i < h; i++) {
92  p[i] = &(p[0][i * w]);
93  }
94  return p;
95 }
96 void free2d_short(short **p) {
97  free(p[0]);
98  free(p);
99 }
100 
101 int **allocate2d_int(size_t h, size_t w){
102  int **p = (int**) malloc(h * sizeof(int*));
103  if (p == NULL) {
104  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
105  return NULL;
106  }
107  p[0] = (int*) malloc(w * h * sizeof(int));
108  if (p[0] == NULL) {
109  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
110  free(p);
111  return NULL;
112  }
113  for (size_t i = 1; i < h; i++) {
114  p[i] = &(p[0][i * w]);
115  }
116  return p;
117 }
118 void free2d_int(int **p) {
119  free(p[0]);
120  free(p);
121 }
122 
123 float **allocate2d_float(size_t h, size_t w){
124  float **p = (float**) malloc(h * sizeof(float*));
125  if (p == NULL) {
126  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
127  return NULL;
128  }
129  p[0] = (float*) malloc(w * h * sizeof(float));
130  if (p[0] == NULL) {
131  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
132  free(p);
133  return NULL;
134  }
135  for (size_t i = 1; i < h; i++) {
136  p[i] = &(p[0][i * w]);
137  }
138  return p;
139 }
140 void free2d_float(float **p) {
141  free(p[0]);
142  free(p);
143 }
144 
145 double **allocate2d_double(size_t h, size_t w){
146  double **p = (double**) malloc(h * sizeof(double*));
147  if (p == NULL) {
148  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
149  return NULL;
150  }
151  p[0] = (double*) malloc(w * h * sizeof(double));
152  if (p[0] == NULL) {
153  fprintf(stderr, "-E- %s line %d: Memory allocation failed.n", __FILE__, __LINE__);
154  free(p);
155  return NULL;
156  }
157  for (size_t i = 1; i < h; i++) {
158  p[i] = &(p[0][i * w]);
159  }
160  return p;
161 }
162 void free2d_double(double **p) {
163  free(p[0]);
164  free(p);
165 }
166 
void free2d_double(double **p)
Free a two-dimensional array created by allocate2d_double.
Definition: allocate2d.c:162
void free2d_schar(signed char **p)
Free a two-dimensional array created by allocate2d_schar.
Definition: allocate2d.c:74
#define NULL
Definition: decode_rs.h:63
float h[MODELMAX]
Definition: atrem_corl1.h:131
void free2d_short(short **p)
Free a two-dimensional array created by allocate2d_short.
Definition: allocate2d.c:96
void free2d_uchar(unsigned char **p)
Free a two-dimensional array created by allocate2d_uchar.
Definition: allocate2d.c:52
void free2d_int(int **p)
Free a two-dimensional array created by allocate2d_int.
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:57
void free2d_float(float **p)
Free a two-dimensional array created by allocate2d_float.
Definition: allocate2d.c:140
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:35
Utility functions for allocating and freeing two-dimensional arrays of various types.
int ** allocate2d_int(size_t h, size_t w)
Allocate a two-dimensional array of type int of a given size.
Definition: allocate2d.c:101
double ** allocate2d_double(size_t h, size_t w)
Allocate a two-dimensional array of type double of a given size.
Definition: allocate2d.c:145
short ** allocate2d_short(size_t h, size_t w)
Allocate a two-dimensional array of type short of a given size.
Definition: allocate2d.c:79
float ** allocate2d_float(size_t h, size_t w)
Allocate a two-dimensional array of type float of a given size.
Definition: allocate2d.c:123
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