Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2443 > unrolled thread

Array of a Class Type that contains an array where the sizes of the array are not known at compile time.

Started byGCRhoads@volcanomail.com
First post2013-01-13 17:38 -0800
Last post2013-01-13 21:39 -0800
Articles 15 — 9 participants

Back to article view | Back to comp.lang.java.help


Contents

  Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-13 17:38 -0800
    Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-01-13 18:56 -0800
      Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 13:36 -0800
        Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-14 16:59 -0500
          Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 20:00 -0800
            Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. GCRhoads@volcanomail.com - 2013-01-14 20:04 -0800
              Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Patricia Shanahan <pats@acm.org> - 2013-01-14 23:44 -0800
              Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 09:07 -0500
            Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-01-15 09:06 +0200
            Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 08:32 -0500
            Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Lew <lewbloch@gmail.com> - 2013-01-15 11:19 -0800
            Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2013-01-15 13:52 -0600
        Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Roedy Green <see_website@mindprod.com.invalid> - 2013-01-15 20:56 -0800
    Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. markspace <markspace@nospam.nospam> - 2013-01-13 19:01 -0800
      Re: Array of a Class Type that contains an array where the sizes of the array are not known at compile time. Lew <lewbloch@gmail.com> - 2013-01-13 21:39 -0800

#2443 — Array of a Class Type that contains an array where the sizes of the array are not known at compile time.

FromGCRhoads@volcanomail.com
Date2013-01-13 17:38 -0800
SubjectArray of a Class Type that contains an array where the sizes of the array are not known at compile time.
Message-ID<f2b43fa0-1da2-4dc4-981b-bb756186050b@googlegroups.com>
class Piece
   {
   public byte type;
   public byte loc;
   }

class Config
   {
   public Config( int n ) { piece = new Piece[ n ]; }
   private Piece[] piece;      // array whose size is not known at compile time
   private int next;
   private int parent;
   }

class QHT
   {
   private Config[] conf;
   public QHT (int m, int n );
   }

In the constructor QHT, I want to allocate to "conf" an array of m Configs
where each Config contains an array of n "Piece"s.  The values of m and n
are not known at compile time only at run time.

How can I do this in Java?

P.S. If you respond by email, please use the following address.
r h o a d s @ c s (dot) r u t g e r s (dot) e d u


[toc] | [next] | [standalone]


#2444

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-01-13 18:56 -0800
Message-ID<atKIs.36232$TH3.9358@newsfe12.iad>
In reply to#2443
On 1/13/13 5:38 PM, GCRhoads@volcanomail.com wrote:
>
> class Piece
>     {
>     public byte type;
>     public byte loc;
>     }
>
> class Config
>     {
>     public Config( int n ) { piece = new Piece[ n ]; }
>     private Piece[] piece;      // array whose size is not known at compile time
>     private int next;
>     private int parent;
>     }
>
> class QHT
>     {
>     private Config[] conf;
>     public QHT (int m, int n );
>     }
>
> In the constructor QHT, I want to allocate to "conf" an array of m Configs
> where each Config contains an array of n "Piece"s.  The values of m and n
> are not known at compile time only at run time.
>
> How can I do this in Java?
>
> P.S. If you respond by email, please use the following address.
> r h o a d s @ c s (dot) r u t g e r s (dot) e d u
>
>
>
Java arrays don't get instantiated at compile time, so you just do it 
the way you create arrays in Java.

Hint: You have an example of part of it in he Config constructor already.
Another Hint: Do your own damned homework.

[toc] | [prev] | [next] | [standalone]


#2447

FromGCRhoads@volcanomail.com
Date2013-01-14 13:36 -0800
Message-ID<fcf46194-fde7-4096-b2d1-490586ccb83a@googlegroups.com>
In reply to#2444
On Sunday, January 13, 2013 9:56:07 PM UTC-5, Daniel Pitts wrote:
> On 1/13/13 5:38 PM, GCRhoads@volcanomail.com wrote:
> 
> >
> 
> > class Piece
> 
> >     {
> 
> >     public byte type;
> 
> >     public byte loc;
> 
> >     }
> 
> >
> 
> > class Config
> 
> >     {
> 
> >     public Config( int n ) { piece = new Piece[ n ]; }
> 
> >     private Piece[] piece;      // array whose size is not known at compile time
> 
> >     private int next;
> 
> >     private int parent;
> 
> >     }
> 
> >
> 
> > class QHT
> 
> >     {
> 
> >     private Config[] conf;
> 
> >     public QHT (int m, int n );
> 
> >     }
> 
> >
> 
> > In the constructor QHT, I want to allocate to "conf" an array of m Configs
> 
> > where each Config contains an array of n "Piece"s.  The values of m and n
> 
> > are not known at compile time only at run time.
> 
> >
> 
> > How can I do this in Java?
> 
> >
> 
> > P.S. If you respond by email, please use the following address.
> 
> > r h o a d s @ c s (dot) r u t g e r s (dot) e d u
> 
> >
> 
> >
> 
> >
> 
> Java arrays don't get instantiated at compile time, so you just do it 
> 
> the way you create arrays in Java.
> 
> 
> 
> Hint: You have an example of part of it in he Config constructor already.
> 
> Another Hint: Do your own damned homework.

This is NOT homework.

Sure I can write.

conf = new Config[ m ]
for (int i=0; i < m; i++)
   conf[i] = new Config( n );

but I need to ensure that the memory returned by the successive calls to
"new Config( n )" occupy contiguous memory locations.  I know how to do this in
C or C++ but the solution uses pointer arithmetic which Java doesn't have.

How can I do this in Java?

[toc] | [prev] | [next] | [standalone]


#2448

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-01-14 16:59 -0500
Message-ID<kd1v4d$u9n$1@dont-email.me>
In reply to#2447
On 1/14/2013 4:36 PM, GCRhoads@volcanomail.com wrote:
>[...]
> Sure I can write.
>
> conf = new Config[ m ]
> for (int i=0; i < m; i++)
>     conf[i] = new Config( n );
>
> but I need to ensure that the memory returned by the successive calls to
> "new Config( n )" occupy contiguous memory locations.  I know how to do this in
> C or C++ but the solution uses pointer arithmetic which Java doesn't have.
>
> How can I do this in Java?

     You cannot.

     To begin with, the memory location of a particular object is
not necessarily constant during the object's lifetime.  The garbage
collector may move it from place to place, may even move it more
than once.  So even if you somehow managed to get two instances to
occupy adjacent chunks of memory it wouldn't last: They'd wander.

     Second, if two objects' memory areas happen to be adjacent at
some time, how could you detect it?  Or, how could you detect that
they are not adjacent?  Can you think of *any* Java code that would
behave differently if objects X and Y were or were not adjacent?
If not, why do you care where the objects happen to reside?

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2449

FromGCRhoads@volcanomail.com
Date2013-01-14 20:00 -0800
Message-ID<2c39444f-5585-4373-a851-6c5aeefa21b3@googlegroups.com>
In reply to#2448
On Monday, January 14, 2013 4:59:38 PM UTC-5, Eric Sosman wrote:
> On 1/14/2013 4:36 PM, GCRhoads@volcanomail.com wrote:
> 
> >[...]
> 
> > Sure I can write.
> 
> >
> 
> > conf = new Config[ m ]
> 
> > for (int i=0; i < m; i++)
> 
> >     conf[i] = new Config( n );
> 
> >
> 
> > but I need to ensure that the memory returned by the successive calls to
> 
> > "new Config( n )" occupy contiguous memory locations.  I know how to do this in
> 
> > C or C++ but the solution uses pointer arithmetic which Java doesn't have.
> 
> >
> 
> > How can I do this in Java?
> 
> 
> 
>      You cannot.
> 
> 
> 
>      To begin with, the memory location of a particular object is
> 
> not necessarily constant during the object's lifetime.  The garbage
> 
> collector may move it from place to place, may even move it more
> 
> than once.  So even if you somehow managed to get two instances to
> 
> occupy adjacent chunks of memory it wouldn't last: They'd wander.
> 
> 
> 
>      Second, if two objects' memory areas happen to be adjacent at
> 
> some time, how could you detect it?  Or, how could you detect that
> 
> they are not adjacent?  Can you think of *any* Java code that would
> 
> behave differently if objects X and Y were or were not adjacent?
> 
> If not, why do you care where the objects happen to reside?
> 
> 
> 
> -- 
> 
> Eric Sosman
> 
> esosman@comcast-dot-net.invalid

I care because it has a large effect on the efficiency of the program.  With contiguous memory locations, the program's memory accesses will have a very high locality of reference resulting in a much faster average memory access time due to the computer's memory cache.

[toc] | [prev] | [next] | [standalone]


#2450

FromGCRhoads@volcanomail.com
Date2013-01-14 20:04 -0800
Message-ID<9e7b5131-a40b-4487-9173-c8c886a99b9d@googlegroups.com>
In reply to#2449
On Monday, January 14, 2013 11:00:14 PM UTC-5, gcrh...@volcanomail.com wrote:
> On Monday, January 14, 2013 4:59:38 PM UTC-5, Eric Sosman wrote:
> 
> > On 1/14/2013 4:36 PM, GCRhoads@volcanomail.com wrote:
> 
> > 
> 
> > >[...]
> 
> > 
> 
> > > Sure I can write.
> 
> > 
> 
> > >
> 
> > 
> 
> > > conf = new Config[ m ]
> 
> > 
> 
> > > for (int i=0; i < m; i++)
> 
> > 
> 
> > >     conf[i] = new Config( n );
> 
> > 
> 
> > >
> 
> > 
> 
> > > but I need to ensure that the memory returned by the successive calls to
> 
> > 
> 
> > > "new Config( n )" occupy contiguous memory locations.  I know how to do this in
> 
> > 
> 
> > > C or C++ but the solution uses pointer arithmetic which Java doesn't have.
> 
> > 
> 
> > >
> 
> > 
> 
> > > How can I do this in Java?
> 
> > 
> 
> > 
> 
> > 
> 
> >      You cannot.
> 
> > 
> 
> > 
> 
> > 
> 
> >      To begin with, the memory location of a particular object is
> 
> > 
> 
> > not necessarily constant during the object's lifetime.  The garbage
> 
> > 
> 
> > collector may move it from place to place, may even move it more
> 
> > 
> 
> > than once.  So even if you somehow managed to get two instances to
> 
> > 
> 
> > occupy adjacent chunks of memory it wouldn't last: They'd wander.
> 
> > 
> 
> > 
> 
> > 
> 
> >      Second, if two objects' memory areas happen to be adjacent at
> 
> > 
> 
> > some time, how could you detect it?  Or, how could you detect that
> 
> > 
> 
> > they are not adjacent?  Can you think of *any* Java code that would
> 
> > 
> 
> > behave differently if objects X and Y were or were not adjacent?
> 
> > 
> 
> > If not, why do you care where the objects happen to reside?
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Eric Sosman
> 
> > 
> 
> > esosman@comcast-dot-net.invalid
> 
> 
> 
> I care because it has a large effect on the efficiency of the program.  With contiguous memory locations, the program's memory accesses will have a very high locality of reference resulting in a much faster average memory access time due to the computer's memory cache.

Also, garbage collection shouldn't be a problem in this case because there won't be any.  All the memory gets allocated at the beginning of the program and everything remains accessible throughout the entire execution of the program.

[toc] | [prev] | [next] | [standalone]


#2452

FromPatricia Shanahan <pats@acm.org>
Date2013-01-14 23:44 -0800
Message-ID<8JmdndQE9PBDlWjNnZ2dnUVZ_s-dnZ2d@earthlink.com>
In reply to#2450
On 1/14/2013 8:04 PM, GCRhoads@volcanomail.com wrote:
> On Monday, January 14, 2013 11:00:14 PM UTC-5,
> gcrh...@volcanomail.com wrote:
...
>> I care because it has a large effect on the efficiency of the
>> program.  With contiguous memory locations, the program's memory
>> accesses will have a very high locality of reference resulting in a
>> much faster average memory access time due to the computer's memory
>> cache.
>
> Also, garbage collection shouldn't be a problem in this case because
> there won't be any.  All the memory gets allocated at the beginning
> of the program and everything remains accessible throughout the
> entire execution of the program.
>

If you need this sort of direct control over memory, and do not need
garbage collection, maybe Java is not the best choice of language for
your application.

Patricia

[toc] | [prev] | [next] | [standalone]


#2454

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-01-15 09:07 -0500
Message-ID<kd3nqk$qm8$1@dont-email.me>
In reply to#2450
On 1/14/2013 11:04 PM, GCRhoads@volcanomail.com wrote:
> On Monday, January 14, 2013 11:00:14 PM UTC-5, gcrh...@volcanomail.com wrote:
>> On Monday, January 14, 2013 4:59:38 PM UTC-5, Eric Sosman wrote:
>>  [...]
>>> If not, why do you care where the objects happen to reside?
>
>> I care because it has a large effect on the efficiency of the program.  With contiguous memory locations, the program's memory accesses will have a very high locality of reference resulting in a much faster average memory access time due to the computer's memory cache.
>
> Also, garbage collection shouldn't be a problem in this case because there won't be any.  All the memory gets allocated at the beginning of the program and everything remains accessible throughout the entire execution of the program.

     "There won't be any" is almost certainly wrong, but "there
won't be much" might be correct.  If so, the objects' memory
areas are likely to stay in their original locations, pretty
much, which means they're likely to be in only a few contiguous
regions.  If they're all created on the same thread, "a few" is
likely to be "one."

     If "likely" and "almost certainly" and "pretty much" aren't
good enough, then again: You're using the wrong language.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2451

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2013-01-15 09:06 +0200
Message-ID<lvip6yorg7.fsf@saunalahti.fi>
In reply to#2449
GCRhoads@volcanomail.com writes:
> On Monday, January 14, 2013 4:59:38 PM UTC-5, Eric Sosman wrote:
>> On 1/14/2013 4:36 PM, GCRhoads@volcanomail.com wrote:

>>      Second, if two objects' memory areas happen to be adjacent at
>> some time, how could you detect it?  Or, how could you detect that
>> they are not adjacent?  Can you think of *any* Java code that would
>> behave differently if objects X and Y were or were not adjacent?

> I care because it has a large effect on the efficiency of the program.
> With contiguous memory locations, the program's memory accesses will have
> a very high locality of reference resulting in a much faster average
> memory access time due to the computer's memory cache.

Anyway, in Java, you will not know where in memory each object is
located. The JVM does all the memory address handling under the hood and
doesn't tell where it decides to allocate memory space for objects.

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#2453

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-01-15 08:32 -0500
Message-ID<kd3lq4$crt$1@dont-email.me>
In reply to#2449
On 1/14/2013 11:00 PM, GCRhoads@volcanomail.com wrote:
> On Monday, January 14, 2013 4:59:38 PM UTC-5, Eric Sosman wrote:
>>[...]
>>       Second, if two objects' memory areas happen to be adjacent at
>> some time, how could you detect it?  Or, how could you detect that
>> they are not adjacent?  Can you think of *any* Java code that would
>> behave differently if objects X and Y were or were not adjacent?
>> If not, why do you care where the objects happen to reside?
>
> I care because it has a large effect on the efficiency of the program.  With contiguous memory locations, the program's memory accesses will have a very high locality of reference resulting in a much faster average memory access time due to the computer's memory cache.

     Then you're using the wrong language.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#2456

FromLew <lewbloch@gmail.com>
Date2013-01-15 11:19 -0800
Message-ID<701be42e-0afa-465f-9e52-8af7bcd7d689@googlegroups.com>
In reply to#2449
GCRh...@volcanomail.com wrote:
> I care because it has a large effect on the efficiency of the program.  

How large? How did you measure that effect?

> With contiguous memory locations, the program's memory accesses will have 
> a very high locality of reference resulting in a much faster average memory access 
> time due to the computer's memory cache.

Really?

I suspect you might be surprised at the actual measurements.

But then, so might I.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2458

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2013-01-15 13:52 -0600
Message-ID<kd4c28$t0q$1@dont-email.me>
In reply to#2449
On 1/14/2013 10:00 PM, GCRhoads@volcanomail.com wrote:
> I care because it has a large effect on the efficiency of the
> program.  With contiguous memory locations, the program's memory
> accesses will have a very high locality of reference resulting in a
> much faster average memory access time due to the computer's memory
> cache.

Are you also properly performing loop tiling to make sure that you 
maximize cache reuse, testing and choosing optimal loop unrolling 
parameters, and otherwise applying superoptimization techniques to your 
code? If the answer to that question was either "no" or "I don't know 
what you're saying," then I'll point out that the default 
characteristics of Java's memory management is good enough performance 
for you. If the answer was "yes", I will point out that you are almost 
certainly using the wrong language.

I will also point out that doing |new int[5][10]| will almost certainly 
be special-cased by the JVM into a dense multidimensional array (I 
recall seeing some mention of multidimensional arrayOops when I was 
poking about in OpenJDK to see if System.arraycopy might be using 
copy-on-write).

-- 
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

[toc] | [prev] | [next] | [standalone]


#2459

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-01-15 20:56 -0800
Message-ID<llccf8pk4kde0q4afr2l7tsfm9p5lk3r2u@4ax.com>
In reply to#2447
On Mon, 14 Jan 2013 13:36:11 -0800 (PST), GCRhoads@volcanomail.com
wrote, quoted or indirectly quoted someone who said :

>but I need to ensure that the memory returned by the successive calls to
>"new Config( n )" occupy contiguous memory locations. 

Java does not do that. See http://mindprod.com/jgloss/array.html

The best you can get is a contiguous array of pointers or ints.  The
objects will be scattered all over. 

I talked with Bill Joy over a decade ago. He was hoping to add this to
Java so you could do things like an efficient Complex implementation,
but I guess he never got his way.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development 
time. 
~ Tom Cargill  Ninety-ninety Law 

[toc] | [prev] | [next] | [standalone]


#2445

Frommarkspace <markspace@nospam.nospam>
Date2013-01-13 19:01 -0800
Message-ID<kcvsdk$eua$1@dont-email.me>
In reply to#2443
On 1/13/2013 5:38 PM, GCRhoads@volcanomail.com wrote:

> In the constructor QHT, I want to allocate to "conf" an array of m Configs
> where each Config contains an array of n "Piece"s.  The values of m and n
> are not known at compile time only at run time.

Two dimensional array?

public class Qht {
   private Piece[][] conf;

   public Qht( int m, int n ) {
     conf = new Piece[m][];
     for( int i = 0; i < m; i++ )  // just wrote "that loop" again
     {
       conf[i] = new Piece[n];
     }
   }
   // rest of class here...
}

[toc] | [prev] | [next] | [standalone]


#2446

FromLew <lewbloch@gmail.com>
Date2013-01-13 21:39 -0800
Message-ID<fdc77c55-c090-4156-91e3-ba16da5ca976@googlegroups.com>
In reply to#2445
markspace wrote:
> GCRhoads wrote:

class Piece 
   { 
   public byte type; 
   public byte loc; 
   } 

class Config 
   { 
   public Config( int n ) { piece = new Piece[ n ]; } 
   private Piece[] piece;      // array whose size is not known at compile time 
   private int next; 
   private int parent; 
   } 

class QHT 
   { 
   private Config[] conf; 
   public QHT (int m, int n ); 
   } 

You should actually copy and paste your code from working code.

Which this isn't. Now if you have issues, then "working" instead 
means "produces the error of interest".

>> In the constructor QHT, I want to allocate to "conf" an array of m Configs

conf = new Config[m];

> 
>> where each Config contains an array of n "Piece"s.  The values of m and n

For each array entry assign it a 
  Config config = new Config(n);

>> are not known at compile time only at run time.

That is intrinsic to constructor and method arguments.

> Two dimensional array?

To GCRhoads: Bear in mind that "two-dimensional array" in Java is actually 
slang. 

In truth there is no "two-dimensional array" exactly. What there is is a 
one-dimensional array of arrays. Each array element of the "outer" array 
points to a separate "inner" array, or to 'null'. Iterating through the 
outer array represents moving to the next row. Iterating through each 
inner array represents moving to the next column. Each inner array might 
be of a different length, or 'null'.

> public class Qht {
>    private Piece[][] conf;

The first dimension, or pair of square brackets, is the outer dimension,
the row dimension. The second dimension is the inner, or column dimension.

>    public Qht( int m, int n ) {
>      conf = new Piece[m][];

'm' rows, each of unknown number of columns or 'null'. (Initially 'null'.)

When you initialize an array using dimensions this way, each array element 
is initialized to the appropriate zero-like value: zero, 'false' or 'null'
depending on the array type. So markspace's example shows how you create 
an array of 'm' elements, each of type 'Piece []', or "array of 'Piece'",
initialized to 'null'.

You have to loop through the outer array to give each element its value.

>      for( int i = 0; i < m; i++ )  // just wrote "that loop" again
>      {
>        conf[i] = new Piece[n];

Once again, he shows creation of an array, this time of length 'n', each 
element of which is initialized to 'null'.

>      }
>    }
> 
>    // rest of class here...
> }

Unfortunately this doesn't quite fit the original question.

You wanted an array of 'Config' objects, yes?

And each 'Config' itself contains an array as a member.

This holds the same knowledge as markspace's data structure. However the use of types is arguably easier to follow is more expressive.

 public void doSomething()
 {
   Config [] configs = new Config [m];

etc.

-- 
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web