File: array_alloc.c

package info (click to toggle)
libgdchart-gd2 0.11.5-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 296 kB
  • sloc: ansic: 3,342; makefile: 287
file content (183 lines) | stat: -rw-r--r-- 5,614 bytes parent folder | download | duplicates (7)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* GDCHART 0.11.2dev  ARRAY_ALLOC.C  Mar 2002 */
/* Copyright Bruce Verderaime 2002-2004 */
/* Contributed in part or whole by Don Fasen and Darren Kent */

#if !defined (__GNUC__)  || defined (__STRICT_ANSI__)

#include <stdio.h>

#include "array_alloc.h"

/*F***************************************************************************
*
* Function Name:         allocate_array_float
*
* Function Prototype:    float	** allocate_array_float (int nr, int nc)
*
* Allocates memory for a two-dimensional array of floats
* referenced as arr[r][c]
*
***************************************************************************
*
*   access array as arr[i1=0-nr][i2=0-nc] = *(&arr[0][0]+nr*i1+i2)
*                       row      col  makes fewest pointer array elements
*                                     assuming Ncol>>Nrow
*  Ex: NC=2
*           _______                  __________
*    **arr |arr[0] |------>arr[0]-->|arr[0][0] |
*          |_______|                |__________|
*          |arr[1] |-----           |arr[0][1] |
*          |_______|     |          |__________|
*          |  .    |      --------->|arr[1][0] |
*          |  .    |                |__________|
*          |_______|                |arr[1][1] |
*          |arr[NR-1]
*          |       |
*         pointer array             data array
*
******************************************************************************
*
* Parameters :
*
* Name               Type           Use       Description
* ----               ----           ---       -----------
* nr                 int          Input     Number of rows
* nc                 int          Input     Number of columns
*
******************************************************************************
*
* Function Return:   float** with allocated memory all set up as float[][].
*
******************************************************************************
*
* Author:  Don Fasen
* Date  :  12/18/98
* Modified: Darren Kent 20/03/2002
*
****************************************************************************F*/
float **
allocate_array_float( int	nr,		/* number of pointer array elements */
					  int	nc )	/* larger number for min memory usage */
{
	int		i;
	float	**arr;

	/* Try to allocate memory for the pointer array, 'nr' pointers-to-float: */
	arr = (float**)malloc( nr * sizeof(float*) );
	/* no test for success ala glib, i.e., if you're outa memory, ... */
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc pointer array in allocate_array_float()\n" );
		exit( 2 );
		}
#endif
	
	/* Now allocate memory for the data array, 'nr * nc' floats: */
	arr[0] = (float*)malloc( nr * nc * sizeof(float) );
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc data array in allocate_array_float()\n" );
		exit( 2 );
		}
#endif
	/* fill in the array pointers to point to successive columns. */
	/* Don't need to do the first (zeroth) one, because it was set up by the data malloc. */
	/* Note that adding 1 to (float*)a.arr[0] adds sizeof(float*) automatically. */

	for (i = 1; i < nr; i++) 
		arr[i] = arr[0] + i*nc;

	return arr;

} /* End of FUNCTION allocate_array_float */

/***********************************************************************************
*
* Same Implementation for a Char array.
*
************************************************************************************/
char **
allocate_array_char( int	nr,
					 int	nc )
{
	int		i;
	char	**arr;

	/* Try to allocate memory for the pointer array, 'nr' pointers-to-chars: */
	arr = (char**)malloc( nr * sizeof(char*) );
	/* no test for success ala glib, i.e., if you're outa memory, ... */
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc pointer array in allocate_array_char()\n" );
		exit( 2 );
		}
#endif
	
	/* Now allocate memory for the data array, 'nr * nc' char: */
	arr[0] = (char*)malloc( nr * nc * sizeof(char) );
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc data array in allocate_array_char()\n" );
		exit( 2 );
		}
#endif
	/* fill in the array pointers to point to successive columns. */
	/* Don't need to do the first (zeroth) one, because it was set up by the data malloc. */
	/* Note that adding 1 to (char*)a.arr[0] adds sizeof(char*) automatically. */

	for (i = 1; i < nr; i++) 
		arr[i] = arr[0] + i*nc;

	return arr;

} /* End of FUNCTION allocate_array_char */

/***********************************************************************************
*
*  Same Implementation for a Int array.
*
************************************************************************************/
int **
allocate_array_int( int	nr,
					int	nc )

{
	int		i;
	int		**arr;

	/* Try to allocate memory for the pointer array, 'nr' pointers-to-ints: */
	arr = (int**)malloc( nr * sizeof(int*) );
	/* no test for success ala glib, i.e., if you're outa memory, ... */
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc pointer array in allocate_array_int()\n" );
		exit( 2 );
		}
#endif
	
	/* Now allocate memory for the data array, 'nr * nc' int: */
	arr[0] = (int*)malloc( nr * nc * sizeof(int) );
#ifdef DBUG
	if( arr == NULL )
		{
		perror( "Unable to malloc data array in allocate_array_int()\n" );
		exit( 2 );
		}
#endif
	/* fill in the array pointers to point to successive columns. */
	/* Don't need to do the first (zeroth) one, because it was set up by the data malloc. */
	/* Note that adding 1 to (int*)a.arr[0] adds sizeof(int*) automatically. */

	for (i = 1; i < nr; i++) 
		arr[i] = arr[0] + i*nc;

	return arr;

} /* End of FUNCTION allocate_array_int */

#endif /* !defined (__GNUC__)  || defined (__STRICT_ANSI__) */