Changing the size of a global array 
Author Message
 Changing the size of a global array

Is is possible to change the size of a global array? I don't ment
unconstrained.For example, say I am loading from a file (with streams), and
I don't know the initial size of the array until I read the header. Because
of the the way my program is structured (using glut calls) I can't use
declare to redimentionalise the array size. Another situation where I need
to reduce size if when I have a list of elements, and I either add to that
list or subtract. in this case I don't want to waste memory be delclaring a
huge array, and I don't want to be swapping from memory to memory. In that
case it would be useful if I could preserve the existing set of elements in
that array (less the ones end ones that have been removed).


Thu, 12 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> Is is possible to change the size of a global array? I don't ment
> unconstrained.For example, say I am loading from a file (with streams),
> and
> I don't know the initial size of the array until I read the header.

no you can't redimension an array in Ada. You can deallocate any space,
and reallocate the space using new. You can also call on realloc (i
think) to copy the data and increase the space at the same time.

If you don't know how much memory you need, wait until you do
know, and then allocate that space from the heap.

Linked lists/some form of dynamically allocated data structure seem
to be the answer to your other problems.

Dale



Thu, 12 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> Is is possible to change the size of a global array? I don't ment
> unconstrained.For example, say I am loading from a file (with streams), and
> I don't know the initial size of the array until I read the header. Because
> of the the way my program is structured (using glut calls) I can't use
> declare to redimentionalise the array size.

If you really can't use a declare block to allocate the array, then you
have a legitimate use for an access type:

   type Unconstrained is array (Index range <>) of Component;
   type Pointer_To_Unconstrained is access all Unconstrained;

   Size : ...;
   Ptr  : Pointer_To_Unconstrained;

   ...

   Size := ...;
   Ptr := new Unconstrained (Index'First .. Size);

   Fill : for I in Ptr'range loop
      Ptr (I) := ...;
   end loop Fill;

Since what you really want here is an array, if you pass the array
around, you should pass the array, not the access value:

   procedure Fill (U : in out Unconstrained);

   Fill (U => Ptr.all);

Quote:
> Another situation where I need
> to reduce size if when I have a list of elements, and I either add to that
> list or subtract. in this case I don't want to waste memory be delclaring a
> huge array, and I don't want to be swapping from memory to memory. In that
> case it would be useful if I could preserve the existing set of elements in
> that array (less the ones end ones that have been removed).

It is unclear that you would really be wasting memory, since the use of
Glut implies a workstation or PC environment, where immense amounts of
memory are available. In an embedded environment such constraints may
apply, but in many cases dynamic allocation is unacceptable. It sounds
as if what you want is some sort of list structure. You can find many
collections of components at

www.adapower.com

I recommend the PragmAda Reusable Components, available there or from

http://home.earthlink.net/~jrcarter010/pragmarc.htm

--
Jeff Carter
"You brightly-colored, mealy-templed, cranberry-smelling, electric
donkey-bottom biters."
Monty python & the Holy Grail



Thu, 12 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> unconstrained.For example, say I am loading from a file (with streams), and
> I don't know the initial size of the array until I read the header. Because

Is it really unacceptable to just keep around the max size array with a
Last_Valid index variable? In a world of cheap expansive memory, that's my
typical choice. But we don't all code in such worlds.

If are using streams, perhaps writing your array out using 'Output instead of
coding the length in a header would allow you to use a declare block (with
'Input).

--
T.E.D.


WWW  - http://www.telepath.com/dennison/Ted/TED.html  ICQ  - 10545591



Thu, 12 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> no you can't redimension an array in Ada. You can deallocate any space,
> and reallocate the space using new. You can also call on realloc (i
> think) to copy the data and increase the space at the same time.

realloc is a C function.  You can probably call it by using pragma
Import, but it's not likely to be a good idea.  In particular,
interaction betweens Ada's new and Unchecked_Deallocation and C's
malloc/realloc/free are undefined; passing memory allocated by Ada's
new to C's realloc may cause Bad Things to happen.  (Or it may work
"properly", but don't count on it.)

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.



Thu, 12 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

There's an easy way that I use in my Ada class:

Read(N);

declare
        The_Data : array(1..N) of Your_Type;
begin
    -- Use The_Data
end;

Or, you could pass N to an internal subprogram,
and declare the array there.

Hope this helps.

John

Quote:

> Is is possible to change the size of a global array? I don't ment
> unconstrained.For example, say I am loading from a file (with streams), and
> I don't know the initial size of the array until I read the header. Because
> of the the way my program is structured (using glut calls) I can't use
> declare to redimentionalise the array size. Another situation where I need
> to reduce size if when I have a list of elements, and I either add to that
> list or subtract. in this case I don't want to waste memory be delclaring a
> huge array, and I don't want to be swapping from memory to memory. In that
> case it would be useful if I could preserve the existing set of elements in
> that array (less the ones end ones that have been removed).

  John_J_Cupak.vcf
< 1K Download


Fri, 13 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> Is is possible to change the size of a global array? I don't ment
> unconstrained.For example, say I am loading from a file (with streams), and
> I don't know the initial size of the array until I read the header.

Why read the file at all? Why not memory-map it and then use a
representation clause for your data structure so you can access the
contents of the file directly. Wouldn't that be more efficient?

Andrew



Mon, 16 Dec 2002 03:00:00 GMT  
 Changing the size of a global array


Quote:
> Is is possible to change the size of a global array?

Have a look at g-table in the GNAT sources

(I am always surprised to find out how many people do not
bother to make themselves aware of the g-xxx.ads files providing
units in the GNAT library hierarchy, quite a bit of useful
stuff there!)

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 16 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> Why read the file at all? Why not memory-map it and then use a
> representation clause for your data structure so you can access the
> contents of the file directly. Wouldn't that be more efficient?

That is something which i'm really interested.
For reference look up slotted allocation in data storage pages in a
database system.

Basically we have a storage memory page containing
a small known number of offsets, some free memory
and a number of records.

Like this:

P0 P1 P2 P3 P4|  free area |  D4 |  D3|
                           ^     ^    ^
                           P0    P4   P3

P1, P2 nil
D4, D3 of different sizes
P0 offset to last unused memory cell.
Different pages contains varying amount of offsets (can be tweaked
depending
on the size of the records)
At item deletion the free space is coalesced into one big space.
Free space is always before the records.

Simply HOW ???

Does it exist an implementation already ? and if so could i get it ??



Thu, 19 Dec 2002 03:00:00 GMT  
 Changing the size of a global array
Quote:


> > Why read the file at all? Why not memory-map it and then use a
> > representation clause for your data structure so you can access the
> > contents of the file directly. Wouldn't that be more efficient?

> That is something which i'm really interested.

...

Quote:
> Simply HOW ???

Brush up on representation clauses in the ada langauge reference manual.
Check your operating system's manual page for details of how the mmap
command is called and what it returns.
Check your compiler's documentation to see how to call mmap (prgama
interface, probably) and whether the returned value would be acceptable
as an access type value.

Then assuming you can use a representation clause describe your entire
file as a discrimiated record with a default value for the discriminant,
create an access type for that record type and convert the result of
mmap to that access type using unchecked conversion. Then simply use the
data.

As the assumption is less than 100% likely from your description
(deleted) you might create a record which describes the first part of
the file and from that extract sufficient information to create a second
access type to a discriminated record whose discriminants you can supply
having read the first record.

Sloppier but more likely to work is to do some ahem
access-type-arithmetic using the result of mmap and the values found in
the first record to produce access values to later components of the
file. ( Each time you call unchecked conversion, take care )

You should probably pepper the code with exception handlers to deal with
the incidences of the file not containing what you expect due to errors
in the record and its representation clause.

best of luck
Andrew



Sat, 28 Dec 2002 03:00:00 GMT  
 Changing the size of a global array

Quote:

> You should probably pepper the code with exception handlers to deal with
> the incidences of the file not containing what you expect due to errors
> in the record and its representation clause.

This advice is misleading.  There's no guarantee that "errors in the
error or its rep clause" will cause any exceptions to get raised.  It's
up to you to

1) Get the rep clauses right.

2) Import data using unconstrained types, and then do error checking
   yourself.



Sat, 28 Dec 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Changing the global size of the graphic elements in a TK program

2. reading the user input, changing size of a 1D array

3. common arrays changing sizes

4. Size(ASSUMED Size ARRAY)

5. Array Selection Start / Array Selection Size

6. Numerical Python array math with different sized arrays

7. Variable Size arrays Was: Indeterminate arrays??

8. Array intrinsics with zero-size array argument

9. Array performance degrades as array size increases

10. changing array values with the same array

11. Change a 2D Array into a 1D Array.

12. Increasing global stack size?

 

 
Powered by phpBB® Forum Software