Board index » cppbuilder » malloc and array

malloc and array


2005-04-25 11:10:17 PM
cppbuilder94
Hi to all,
How can I initialize an array of four dimention using malloc?
I know that I can I do that:
float *miarray;
...
miarray = (float *) malloc (size * sizeof(float));
...
but if I want that my array have four dimension what can I do?
how can I do that?
(I like to do miarray[n][m][x][y])
thanks in advance
 
 

Re:malloc and array

On 25 Apr 2005 08:10:17 -0700, Orlando Lopez wrote:
Quote
miarray = (float *) malloc (size * sizeof(float));
Why do you want to use malloc rather than new?
why not use vector of vector of vector of double?
--
Good luck,
liz
 

Re:malloc and array

Because you used malloc I conclude that you are programming in C and
not in C++.
The provisions for simple declaration of multi-dimensional arrays in C
and C++ extend only to items declared as base-type entities with all
dimensions specified and not to allocated items using pointers. To do
it with an allocated item one must allocate the data areas for each
index. For example: (not passed through the compiler so beware of
typos)
-------------------
#define DIM1 12
#define DIM2 3
#define DIM3 6
#define DIM4 8
float *miarray[DIM1][DIM2][DIM3];
(void *) miarray = malloc(DIM1 * DIM2 * DIM3 * sizeof(float *));
if (miarray)
{
int i;
int j;
int k;
for (i = 0; i < DIM1; ++i)
{
for (j = 0; j < DIM2; ++j)
{
for (k = 0; k < DIM3; ++k)
{
(void *) miarray[i][j][k] = malloc(DIM4 * sizeof(float));
if (!miarray[i][j][k])
{
handle the allocation error
}
}
}
}
}
else
{
handle the allocation error
}
-------------------
When freeing the array, each item that is a value returned from malloc
should be passed to free with the base name, miarray, freed last.
I see that you are using float. All math operations are performed on
doubles and floats are converted to doubles prior to the operation or
function call and converted back afterwards. As floats have only
about 6 decimal digits of precision, using floats implies that you
have little interest in the actual value. It is possible that you
need to be using double instead of float.
. Ed
Quote
Orlando Lopez wrote in message
news:426d0859$ XXXX@XXXXX.COM ...

Hi to all,
How can I initialize an array of four dimention using malloc?
I know that I can I do that:
float *miarray;
...
miarray = (float *) malloc (size * sizeof(float));
...

but if I want that my array have four dimension what can I do?
how can I do that?
(I like to do miarray[n][m][x][y])
 

{smallsort}

Re:malloc and array

"Orlando Lopez" < XXXX@XXXXX.COM >wrote in message
Quote
How can I initialize an array of four dimention using malloc?
Try multiplying the dimensions together:
miarray = (float *) malloc(sizeof(float) * (n*m*x*y));
Alternatively, allocate each dimension separately:
float ****miarray = miarray = (float ****) malloc(sizeof(float***) * n);
for(int i = 0; i < n; ++i)
{
miarray[i] = (float ***) malloc(sizeof(float**) * m);
for(int j = 0; j < m; ++j)
{
miarray[i][j] = (float **) malloc(sizeof(float*) * x);
for(int k = 0; k < x; ++k)
miarray[i][j][k] = (float *) malloc(sizeof(float) * y);
}
}
//...
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
for(int k = 0; k < x; ++k)
free(miarray[i][j][k]);
free(miarray[i][j]);
}
free(miarray[i]);
}
free(miarray);
Which you can then translate into an array of std::vector instances for
better memory management:
#include <vector>
typedef std::vector<float>FloatVector1Dim;
typedef std::vector<FloatVector1Dim>FloatVector2Dim;
typedef std::vector<FloatVector2Dim>FloatVector3Dim;
typedef std::vector<FloatVector3Dim>FloatVector4Dim;
FloatVector4Dim miarray(n);
for(int i = 0; i < n; ++i)
{
miarray[i].resize(m);
for(int j = 0; j < m; ++j)
{
miarray[i][j].resize(x);
for(int k = 0; k < x; ++k)
miarray[i][j][k].resize(y);
}
}
Gambit