-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocate_2d.cc
89 lines (81 loc) · 1.9 KB
/
allocate_2d.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdlib.h>
// these are all routines for allocation of a 2d array arr of size arr[n1][n2] for different data types
// of course, C++ template would be more elegant, but this way we can get by with just C compiler
char ** allocate_2d_c(int n1, int n2)
{
int i;
char ** p = NULL;
p = (char **) malloc(n1*sizeof(char *));
if(p==NULL) return NULL;
p[0] = (char *) malloc(n1*n2*sizeof(char));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};
float ** allocate_2d_f(int n1, int n2)
{
int i;
float ** p = NULL;
p = (float **) malloc(n1*sizeof(float *));
if(p==NULL) return NULL;
p[0] = (float *) malloc(n1*n2*sizeof(float));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};
double ** allocate_2d_d(int n1, int n2)
{
int i;
double ** p = NULL;
p = (double **) malloc(n1*sizeof(double *));
if(p==NULL) return NULL;
p[0] = (double *) malloc(n1*n2*sizeof(double));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};
int ** allocate_2d_i(int n1, int n2)
{
int i;
int ** p = NULL;
p = (int **) malloc(n1*sizeof(int *));
if(p==NULL) return NULL;
p[0] = (int *) malloc(n1*n2*sizeof(int));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};
short ** allocate_2d_s(int n1, int n2)
{
int i;
short ** p = NULL;
p = (short **) malloc(n1*sizeof(short *));
if(p==NULL) return NULL;
p[0] = (short *) malloc(n1*n2*sizeof(short));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};
unsigned short ** allocate_2d_us(int n1, int n2)
{
int i;
unsigned short ** p = NULL;
p = (unsigned short **) malloc(n1*sizeof(unsigned short *));
if(p==NULL) return NULL;
p[0] = (unsigned short *) malloc(n1*n2*sizeof(unsigned short));
if(p[0]==NULL) return NULL;
for(i=1; i<n1; i++) {
p[i] = &(p[0][i*n2]);
}
return p;
};