triangular array and matrix in Java VS C

J

josh

Hi, if in Java we can create a triangular array like in the following
listing:

int a[][] = new int[2][];
a[0] = new int[3]; // 3 cols
a[1] = new int[4]; // 4 cols

can we assert that it's assimilable at the C array of pointers?

and a non-triangular array like a matrix is assimilable at the C
pointer to array?

How, work the compiler behind the scenes?

Thanks
 
O

Oliver Wong

josh said:
Hi, if in Java we can create a triangular array like in the following
listing:

int a[][] = new int[2][];
a[0] = new int[3]; // 3 cols
a[1] = new int[4]; // 4 cols

can we assert that it's assimilable at the C array of pointers?

and a non-triangular array like a matrix is assimilable at the C
pointer to array?

How, work the compiler behind the scenes?

I don't know what you mean by "assimilable" in this context, but arrays
in Java are a kind of object. In the above code, "a" is reference pointing
to an object of type "int[][]". "a[0]" is an expression which resolves to a
reference pointing to an object of type "int[]". There is no requirement in
Java that objects take up a contiguous region of memory.

- Oliver
 
C

Chris Uppal

josh said:
int a[][] = new int[2][];
a[0] = new int[3]; // 3 cols
a[1] = new int[4]; // 4 cols

can we assert that it's assimilable at the C array of pointers?

If you built a triangular array in C, using the "natural" representation as an
array of pointers, then it would be quite similar to the above, yes.

and a non-triangular array like a matrix is assimilable at the C
pointer to array?

Depends. A rectangular array in C can be constructed in two ways. One way is
just like the triangular array, where the "main" array contains pointers to
separate arrays for each row (or is it column -- I can never remember ?) The
other way is that the C compiler will build a single block of memory of size
N*M, and each row/column access will be converted by the compiler into a single
access to the correct cell in that array (using a bit of arithmetic to help).

The second form has the advantage that it doesn't require a double indirection
to get at the data.

If you create a two dimensional array in Java, then you are using the
equivalent of the first form; there is no built-in equivalent to the second.
So if you need the extra performance (which sometimes happens, but not often)
then you have to use a single large array and code up the arithmetic yourself.

-- chris
 
J

josh

Chris Uppal ha scritto:
Depends. A rectangular array in C can be constructed in two ways. One way is
just like the triangular array, where the "main" array contains pointers to
separate arrays for each row (or is it column -- I can never remember ?)

in an array of pointers there is one row and the colums numbers are the
pointers we're
referring to
If you create a two dimensional array in Java, then you are using the
equivalent of the first form; there is no built-in equivalent to the second.
So if you need the extra performance (which sometimes happens, but not often)
then you have to use a single large array and code up the arithmetic yourself.

how can in Java code up the arithmetic myself if I can't access the
real address of the array?
 
O

Oliver Wong

josh said:
Chris Uppal ha scritto:


how can in Java code up the arithmetic myself if I can't access the
real address of the array?

You'd write a function which maps from one coordinate space to the
other.

second
index
|0 1 2 3
-+-------
first 0|0 1 2
index 1|3 4 5 6


So you'd write a function which, given (0,0), returns 0, and given (1,2)
returns 5, for example.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top