Multi-dimensional array using dynamic memory allocation
Author Message Multi-dimensional array using dynamic memory allocation

Hello guys,

I would like to declare a multi-dimensional array using dynamic memory
allocation. While I have the code for one dimension,

int x_length=5;
int *one_dimension_array;
int (int *)malloc(x_length*sizeof(int));
/* test */
one_dimension_array=3453;

I just can't work out how to add the second dimension so I can write
something like

two_dimensions_array=3453;

Thank you for helping,

regards,
Enrique
--

Sat, 20 Nov 2004 14:09:48 GMT  Multi-dimensional array using dynamic memory allocation
On 04 Jun 2002 06:09:48 GMT, Enrique Vidal Snchez said:

Quote:
> Hello guys,

> I would like to declare a multi-dimensional array using dynamic memory
> allocation. While I have the code for one dimension,

Hi,

This information (and more!) is in section 6 of the FAQ
http://www.eskimo.com/~scs/C-faq/top.html - in particular
question 6.16 will interest you.

Cheers,
Dave.

--
David Neary,
E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html
--

Sun, 21 Nov 2004 08:48:26 GMT  Multi-dimensional array using dynamic memory allocation

Quote:
> I just can't work out how to add the second dimension so I can write
> something like
> two_dimensions_array=3453;

See the C FAQ (posted here regularly), Question 6.16.  It answers this
in great detail.
--

Even if all the snow were burnt, ashes would remain.
--

Sun, 21 Nov 2004 08:48:29 GMT  Multi-dimensional array using dynamic memory allocation

Quote:
> Hello guys,

> I would like to declare a multi-dimensional array using dynamic memory
> allocation. While I have the code for one dimension,

> int x_length=5;
> int *one_dimension_array;
> int (int *)malloc(x_length*sizeof(int));
> /* test */
> one_dimension_array=3453;

> I just can't work out how to add the second dimension so I can write
> something like

> two_dimensions_array=3453;

In C99, you can simply do this:

int y_length=6, x_length=5;

int two_dimension_array[y_length][x_length];

two_dimension_array = 3453;

thus avoiding the need for malloc. If you still want to use malloc
(presumably because you need a longer lifetime for the array), you could do:

int y_length=6, x_length=5;

int (*two_dimension_array)[x_length];

two_dimension_array = malloc(y_length * x_length * sizeof(int));

two_dimension_array = 3453;

In C90, you're limited by the fact that all array dimensions other than the
first must be constants, so you can only do:

int y_length=6;
#define X_LENGTH 5

int (*two_dimension_array)[X_LENGTH];

two_dimension_array = malloc(y_length * X_LENGTH * sizeof(int));

two_dimension_array = 3453;

This is somewhat annoying, but prevented the need for the compiler to create
"hidden" variables storing the dimensions of array types. The standard work
around is to do the subscripting manually:

int y_length=6, x_length=5;

int *two_dimension_array;

two_dimension_array = malloc(y_length * x_length * sizeof(int));

two_dimension_array[3*x_length + 2] = 3453;

--
Kevin Bracey
http://www.bracey-griffith.freeserve.co.uk/
--

Sun, 21 Nov 2004 08:48:30 GMT  Multi-dimensional array using dynamic memory allocation

Quote:
> Hello guys,

> I would like to declare a multi-dimensional array using dynamic memory
> allocation. While I have the code for one dimension,

> int x_length=5;
> int *one_dimension_array;
> int (int *)malloc(x_length*sizeof(int));
> /* test */
> one_dimension_array=3453;

> I just can't work out how to add the second dimension so I can write
> something like

see http://www.eskimo.com/~scs/C-faq/q6.16.html as well as
http://www.eskimo.com/~scs/C-faq/q7.7.html

hth.

sinan
--

Sun, 21 Nov 2004 08:48:33 GMT  Multi-dimensional array using dynamic memory allocation

Quote:
> Hello guys,

> I would like to declare a multi-dimensional array using dynamic memory
> allocation. While I have the code for one dimension,

> int x_length=5;
> int *one_dimension_array;
> int (int *)malloc(x_length*sizeof(int));
> /* test */
> one_dimension_array=3453;

> I just can't work out how to add the second dimension so I can write
> something like

> two_dimensions_array=3453;

It's not you, it's C.  An array of arrays does not decay into
a pointer to a pointer so there's no way of dynamically allocating
memory and trying to reference it with double subscripts.  The best
you can do is dynamically allocate enough memory and keep track of

two_dimensions_array = 3453;

you reference

two_dimensions_array[2*ncolumns + 3] = 3453;

Hope that helps :).

--Jeff Turner
Senior Engineer
No-wei Firmware
No-wei, leaders in firmware design and debug for over a fortnight.

Quote:

> Thank you for helping,

> regards,
> Enrique

--

Sun, 21 Nov 2004 22:57:28 GMT  Multi-dimensional array using dynamic memory allocation
On 04 Jun 2002 06:09:48 GMT, Enrique Vidal Snchez

Quote:

>Hello guys,

>I would like to declare a multi-dimensional array using dynamic memory
>allocation. While I have the code for one dimension,

>int x_length=5;
>int *one_dimension_array;
>int (int *)malloc(x_length*sizeof(int));
>/* test */
>one_dimension_array=3453;

>I just can't work out how to add the second dimension so I can write
>something like

>two_dimensions_array=3453;

>Thank you for helping,

>regards,
>Enrique

See the C FAQ (http://www.eskimo.com/~scs/C-faq/top.html), topic 6.16

<<Remove the del for email>>
--

Sun, 21 Nov 2004 22:57:37 GMT  Multi-dimensional array using dynamic memory allocation

Quote:

> It's not you, it's C.  An array of arrays does not decay into
> a pointer to a pointer so there's no way of dynamically allocating
> memory and trying to reference it with double subscripts.

There is.  As the FAQ answer which has been referred to earlier shows.

You can allocate an array of pointers dynamically, and point them to
where you want.  For those, subscripts work exactly as expected.
sizeof() or similar, OTOH, won't, so you'll have to carry around

--

Even if all the snow were burnt, ashes would remain.
--

Mon, 22 Nov 2004 03:10:33 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages