Dynamic arrays????? 
Author Message
 Dynamic arrays?????

I have encountered yet another problem while struggling to complete my graphics
library. I have code that looks like this:

type
     _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}
     ImagePtr = ^_BYTEARRAY;

     BITMAP = object
                 ....
                 Image : ImagePtr;
     end;
...

procedure BITMAP.Create(w, h : integer); {Creates a bitmap object with size
width by height}
begin
...
     GetMem(Image, w*h); {Allocate enough memory to store each pixel}
end;

Then I should be able to access the array like this:
Image^[addr] := c;
In order to set a pixel at (x,y), where addr := y*BytesPerScanLine+x.
Basically what I'm doing is writing a bitmap type that can be used as a memory
bitmap and then blitted to the screen. I have derived the types Mode13hScreen
and VesaScreen from the Bitmap type, and they work perfectly by plotting
directly to the screen. The memory bitmap doesn't work.

Here's my problem:

On the line      _BYTEARRAY = array[0..maxlongint] of byte;
I get an error that says 'ordinal data type expected.' Since when is longint
not an ordinal data type?
I've tried saying array[0..0] of byte, and if I do that I can allocate the
memory but I can't say Image^[addr] := c unless addr is 0. This is very
incovenient.
How can I fix my problem? Thanks ahead of time for your help.

(BTW, if any of you have used the Allegro graphics library in C by Shawn
Hargreaves, written for DJGPP, you should have an idea of what I want this to
do - all procedures can be used on any bitmap, whether screen or memory.
Blitting from memory bitmap to screen bitmap works just like blitting from one
memory bitmap to another)



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

Quote:
> I have encountered yet another problem while struggling to complete my graphics
> library. I have code that looks like this:
> type _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}

   Regardless of how you intend to use it, TP/BP sees this data type as
too large for anything it can declare or allocate - it's obviously more
than 64K bytes, and TP/BP cannot work with any single data variable or
item larger than 64K bytes.  You'll have to find some other way to work
with your data (and I don't work with graphics, etc., so I don't know
what's done by those who so).


Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

: > I have encountered yet another problem while struggling to complete my graphics
: > library. I have code that looks like this:
: > type _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}

:    Regardless of how you intend to use it, TP/BP sees this data type as
: too large for anything it can declare or allocate - it's obviously more
: than 64K bytes, and TP/BP cannot work with any single data variable or
: item larger than 64K bytes.  You'll have to find some other way to work
: with your data (and I don't work with graphics, etc., so I don't know
: what's done by those who so).

 I'm not sure of the origional query but you could use this method to use
big arrays, either splitting them up into a few, ie. if y<100 use
BYTEARRAY 1 else use byte array 2 (for height=200 anyway). OR you could
use the array as a pointer, turn off range checking and allocate the
memory using DOS. All attempts to access an index >64k in TP will die,
but in BP I think it is possible. Or you could write the code in ASM
or some other Pascal compiler like P32 or TMT.

 Any of this help?

--




Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????



Quote:
>I have encountered yet another problem while struggling to complete my graphics
>library. I have code that looks like this:

>type
>     _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}
>     ImagePtr = ^_BYTEARRAY;

>     BITMAP = object
>                 ....
>                 Image : ImagePtr;
>     end;
>...

>procedure BITMAP.Create(w, h : integer); {Creates a bitmap object with size
>width by height}
>begin
>...
>     GetMem(Image, w*h); {Allocate enough memory to store each pixel}
>end;

>Then I should be able to access the array like this:
>Image^[addr] := c;
>In order to set a pixel at (x,y), where addr := y*BytesPerScanLine+x.

>Here's my problem:

>On the line      _BYTEARRAY = array[0..maxlongint] of byte;
>I get an error that says 'ordinal data type expected.' Since when is longint
>not an ordinal data type?

anything above 0..64K-1 generates this error. This is because the
compiler works out that you would be unable to use New and GetMem to
allocate the structure as BP/TP works using 64K segmentation.

By the way, allocating 0..2147483647 bytes is around 2048Mb !

Quote:
>I've tried saying array[0..0] of byte, and if I do that I can allocate the
>memory but I can't say Image^[addr] := c unless addr is 0. This is very
>incovenient.

If you declare a type _bytearray =  array[0..0] of byte, you
could then declare your variable as var bptr : ^bytearray and use New /
Getmem to allocate structures under 64K up to the size you need. This
was the approach I had to take when writing database management
routines.

After allocating memory in sections you *must* turn range checking off
to do Image^[addr] := c *and* code to make sure that you don't overrun
the block you allocated for Image^

Are you looking at protected mode or real/XMS to allocate your data
structures? Further help may be dependent on which target youare looking
towards.

--

Never curse the Crocodile's mother before crossing the river



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????



Quote:
> I have encountered yet another problem while struggling to complete my
graphics
> library. I have code that looks like this:

> type
>      _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}
>      ImagePtr = ^_BYTEARRAY;

>      BITMAP = object
>                  ....
>                  Image : ImagePtr;
>      end;
> ...

I hope, I understood the problem, so why don't you just put there:
BYTEARR = array[0..0] of Byte;

???
I see, you did this but did you put the range check compiler option off?

{$R-}

Hope 2 hyve helped u,

Billy



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????


Wed, 29 Apr 1998 21:47:25 in comp.lang.pascal.borland, SWars Matt

Quote:

>I have encountered yet another problem while struggling to complete my graphics
>library. I have code that looks like this:

>type
>     _BYTEARRAY = array[0..maxlongint] of byte; {Gives me an error}

See  http://www.merlyn.demon.co.uk/pas-extn.htm#VLarrs and thereabouts;
also TSFAQP #14.

Declare [0..X] for X around $FFF0; have range-checking ON.

--

  Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

Quote:


Matt

>>...
>>Here's my problem:

>>On the line      _BYTEARRAY = array[0..maxlongint] of byte;
>>I get an error that says 'ordinal data type expected.'
>>...

>anything above 0..64K-1 generates this error. This is because the
>compiler works out that you would be unable to use New and GetMem to
>allocate the structure as BP/TP works using 64K segmentation.
>...

>If you declare a type _bytearray =  array[0..0] of byte, you
>could then declare your variable as var bptr : ^bytearray and use New
/
>Getmem to allocate structures under 64K up to the size you need.
>...

Is going from MaxLongInt to 0 not a little extreme? You have to turn
off range checking and unless you really like typing {$R you'll have
it turned off for the whole unit (or program). Would it not make more
sense to set the upper limit to its maximum legal value: 64K-1? Range
checking would still not work on the array of course, since you
probably don't have exactly 65535 elements, but at least you have the
option of turning it on for the rest of the program.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

Well, thanks for all the suggestions, but I figured out how to solve my own
problem, almost. I didn't want to use a limit of 64k but rather of about 2
megs, for large (e.g. 1600x1200) bitmaps. The way that I did this was:

type
    _BYTE_ARRAY = array[1..2000] of byte; {valid limit}
    ^_BYTE_ARRAY = ^_BYTE_ARRAY;
    ImagePtr = array[1..2000] of ^_BYTE_ARRAY;

This makes it even easier to plot a point, just by saying Image[x]^[y] := c.

I *still* have a problem. I run out of heap space trying to allocate 800x600
bitmaps. Is there a way I can use disk space as virtual memory from within a
Pascal program that isn't hopelessly complicated? (My final project is due in a
couple of weeks and I have several things to get done so I don't want to spend
too much time on this part).

Thanks for your help.



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

: Well, thanks for all the suggestions, but I figured out how to solve my own
: problem, almost. I didn't want to use a limit of 64k but rather of about 2
: megs, for large (e.g. 1600x1200) bitmaps. The way that I did this was:
:
: type
:     _BYTE_ARRAY = array[1..2000] of byte; {valid limit}
:     ^_BYTE_ARRAY = ^_BYTE_ARRAY;
:     ImagePtr = array[1..2000] of ^_BYTE_ARRAY;
:
: This makes it even easier to plot a point, just by saying Image[x]^[y] := c.

       Maybe you discovered something worthwhile. :-)

: I *still* have a problem. I run out of heap space trying to allocate 800x600
: bitmaps. Is there a way I can use disk space as virtual memory from within a
: Pascal program that isn't hopelessly complicated? (My final project is due
: in a couple of weeks and I have several things to get done so I don't
: want to spend too much time on this part).

       I think max_heap is 655350 or so.

       You said you have 64 megs, install a RAMDRIVE
as DEVICEHIGH and write those pointers to a file on
that drive.
       It will be fast, but you will have to include
code to save that file to a hard diak before terminating
the program to be sure you don't lose it when you shut
off the computer.

Ken Fischer

---



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????



Quote:
>Well, thanks for all the suggestions, but I figured out how to solve my own
>problem, almost.
>I *still* have a problem. I run out of heap space trying to allocate 800x600
>bitmaps. Is there a way I can use disk space as virtual memory from within a
>Pascal program that isn't hopelessly complicated?

The first part of this thread has expired from my local cache so not
sure what compiler version you have. I assume you are not uing BP7 and
protected mode.

Your best option is probably to use XMS memory and page in and out of
the heap. Certainly this will be fairly quick. There are plenty of XMS
units around on the net, in SWAG, and there's one on my pages as well -
http://www.pedt.demon.co.uk/

It is possible to write the image to disk and use Seek and Blockread /
Blockwrite to page 64K sections but this will be slow.

As I see it is for a project, make sure which solution you use is
machine independent - ie trying to use a RAMdisk or EMS memory which may
not be available on the target machine.

--

Never curse the Crocodile's mother before crossing the river



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic arrays?????

Quote:

>I *still* have a problem. I run out of heap space trying to allocate 800x600
>bitmaps. Is there a way I can use disk space as virtual memory from within a
>Pascal program that isn't hopelessly complicated? (My final project is due in a
>couple of weeks and I have several things to get done so I don't want to spend
>too much time on this part).

The simplest solution would be to handle the file in blocks.  You
could then convert "image" to a function that returned a pointer.
Instead of writing Image[x]^[y] := c you could, depending on how you
implement the function, write Image(x,y)^ := c or Image(x)^[y] := c

Image would determine if the required byte or array was in memory.  If
it wasn't, it would release the oldest block, writing that block to
disk if it had been altered and load the desired block from disk.  Of
course it would also have to be concerned with expanding the file
should you request a location beyond the physical end of the existing
file.  When all the house keeping and admin was done, Image would then
return a pointer to the appropriate location.  

You might consider using two functions, one when you only wish to read
from a location and the other when you need to update a location.
That would make it easier to keep track of blocks that had been
altered so that you wouldn't have to write everything back to disk
each time a new block was needed.

BLOCKIO.ZIP contains source for an object that does what you want.
I've used it to access multiple files some of which were 3-400 meg in
size.  The unit also uses UTIL and FILES, so you'll need copies of
them as well.  Follow links at: http://users.southeast.net/~rdonais/

If your project requires you write all code yourself, then it would
probably be quicker for you to design something tailored to your
specific requirements.  Since time is a constraint, I'd suggest you
start by using a single buffer.  Maybe for a first solution you could
use a tBufStream from the object unit.  Then if you have time, you
could derive a new tBufStream object that used a pair of buffers.

    ...red

--
Support the anti-Spam amendment
  Join at http://www.cauce.org/



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. dynamic array

2. Dynamic array in database

3. How to store dynamic array to memofield?

4. Dynamic Array puzzle

5. How to declare dynamic array in fpc ??

6. dynamic arrays

7. Memory access; relative position (dynamic array)

8. Heap error while allocating dynamic array in protected.

9. How do you do a dynamic array in oascal

10. Question: Dynamic arrays in Turbo Pascal

11. Dynamic arrays...

12. dynamic arrays

 

 
Powered by phpBB® Forum Software