File: malloc_multidimarray.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (138 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download | duplicates (4)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "openmx_common.h"

#ifdef DEBUG
#undef DEBUG
#endif

void *malloc_multidimarray(char *type, int N, int *size)
{
   int i;
   void **ret,*r;

   if (strcasecmp(type,"double")==0) {
      if (N==1) {
         r=  (void*)malloc(sizeof(double)*size[0]);
#ifdef DEBUG
         printf("malloc_multidimarray: %x  (size=%d)\n",r,size[0]);
#endif
         return r;
      }
      else {
          ret=(void**)malloc(sizeof(double*)*size[0]);
#ifdef DEBUG
          printf("malloc_multidimarray: %x\n next size=%d\n",ret,size[0]);
#endif
          for (i=0;i<size[0];i++) {
              ret[i]=(void*)malloc_multidimarray(type,N-1,&size[1]);
          }
          return ret;
      }
  }
  else if (strcasecmp(type,"dcomplex")==0) {
      if (N==1) {
          r=(void*)malloc(sizeof(dcomplex)*size[0]);
#ifdef DEBUG
          printf("malloc_multidimarray: %x  (size=%d)\n",r,size[0]);
#endif
          return r;
      }
      else {
          ret=(void**)malloc(sizeof(dcomplex*)*size[0]);
#ifdef DEBUG
          printf("malloc_multidimarray: %x\n next size=%d\n",ret,size[0]);
#endif
          for (i=0;i<size[0];i++) {
              ret[i]=(void*)malloc_multidimarray(type,N-1,&size[1]);
          }
          return ret;
      }
  }
  else if (strcasecmp(type,"float")==0) {
      if (N==1) {

          return (void*)malloc(sizeof(float)*size[0]);
      }
      else {
          ret=(void**)malloc(sizeof(float*)*size[0]);
          for (i=0;i<size[0];i++) {
              ret[i]=(void*)malloc_multidimarray(type,N-1,&size[1]);
          }
          return ret;
      }
  }
  else if (strcasecmp(type,"int")==0) {
      if (N==1) {

          return (void*)malloc(sizeof(int)*size[0]);
      }
      else {
          ret=(void**)malloc(sizeof(int*)*size[0]);
          for (i=0;i<size[0];i++) {
              ret[i]=(void*)malloc_multidimarray(type,N-1,&size[1]);
          }
          return ret;
      }
  }
  else if (strcasecmp(type,"char")==0) {
      if (N==1) {

          return (void*)malloc(sizeof(char)*size[0]);
      }
      else {
          ret=(void**)malloc(sizeof(char*)*size[0]);
          for (i=0;i<size[0];i++) {
              ret[i]=(void*)malloc_multidimarray(type,N-1,&size[1]);
          }
          return ret;
      }
  }
  else {
      printf("not supported type=%s\n",type);
      exit(10);
  }
}


void free_multidimarray(void **p,  int N, int *size)
{
   int i;

   if (N>1) {
     for (i=(size[0]-1);i>=0;i--){
       free_multidimarray(p[i],N-1,&size[1]);
       p[i]=NULL;
     }
   }
#if DEBUG
   printf("free_multidimarray: %x \n",p);
#endif
   free(p);

}

#if 0
main()
{
   int N, size[10];
   double *v1,***v3;
   int i,j,k;

   N=1;
   size[0]=5;

   N=3;
   size[1]=2;
   size[2]=3; 
   v3=(double***)malloc_multidimarray("double",N,size);
   for (i=1;i<5;i++) for (j=1;j<2;j++) for(k=1;k<3;k++)  v3[i][j][k]=100*i+10*j+k;
   for (i=1;i<5;i++) for (j=1;j<2;j++) for(k=1;k<3;k++)  printf("%lf\n",v3[i][j][k]);

  free_multidimarray(v3,N,size);


}
#endif