sushant_ocv
Posts: 15
Joined: Mon May 20, 2019 8:14 am

initializing multi dim array R PI

Tue May 28, 2019 10:22 am

Can someone provide an example to initialize multi dimensional array.

User avatar
topguy
Posts: 6642
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: initializing multi dim array R PI

Tue May 28, 2019 10:44 am


radapa
Posts: 1
Joined: Sat May 30, 2020 8:18 pm

Re: initializing multi dim array R PI

Sat May 30, 2020 8:22 pm

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/

Heater
Posts: 16344
Joined: Tue Jul 17, 2012 3:02 pm

Re: initializing multi dim array R PI

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.
Memory in C++ is a leaky abstraction .

jalih
Posts: 127
Joined: Mon Apr 15, 2019 3:54 pm

Re: initializing multi dim array R PI

Mon Jun 01, 2020 1:25 pm

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...

Return to “Java”