Page 1 of 1

initializing multi dim array R PI

Posted: Tue May 28, 2019 10:22 am
by sushant_ocv
Can someone provide an example to initialize multi dimensional array.

Re: initializing multi dim array R PI

Posted: Tue May 28, 2019 10:44 am
by topguy

Re: initializing multi dim array R PI

Posted: Sat May 30, 2020 8:22 pm
by radapa
Multidimensional array initialization using dynamic memory allocation in C Programming

a = (int **)malloc(rows * sizeof(int *));

for (i = 0; i < rows; i++)
{
a = (int *)malloc(cols * sizeof(int));
}

Source: https://kodebinary.com/c-program-to-dyn ... ry-arrays/

Re: initializing multi dim array R PI

Posted: Sun May 31, 2020 10:49 pm
by Heater
The question is about Java radapa.

Besides your example is not even a 2d array as far as I am concerned. It's a 1D array of pointers to other 1D arrays. Rather than being a 2d array in contiguous memory.

If you declare you array correctly you can allocate it in one go like so:

Code: Select all

array = int (*)[COLS] malloc(ROWS*COLS*sizeof(int))
And then initialize it with a single call to memset or whatever you like.

A proper contiguous 2d array will be much faster for large arrays.

Re: initializing multi dim array R PI

Posted: Mon Jun 01, 2020 1:25 pm
by jalih
Heater wrote:
Sun May 31, 2020 10:49 pm
The question is about Java radapa.

Besides your example is not even a 2d array as far as I am concerned. It's a 1D array of pointers to other 1D arrays. Rather than being a 2d array in contiguous memory.

If you declare you array correctly you can allocate it in one go like so:

Code: Select all

array = int (*)[COLS] malloc(ROWS*COLS*sizeof(int))
And then initialize it with a single call to memset or whatever you like.

A proper contiguous 2d array will be much faster for large arrays.
Damn, that looks ugly... ;)

PL/I got it right over 60 years ago...

Code: Select all

dcl p pointer;
dcl array(100, 100) fixed bin(31) based(p);
allocate array;
Sorry, for side tracking from Java...