Memory problems with user-defined types

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
ThemePark
Coder
Posts: 17
Joined: Wed Aug 22, 2007 6:37 pm

Memory problems with user-defined types

Post by ThemePark » Thu Aug 23, 2007 6:21 pm

I am trying to make a user-defined type to help me define an image. To do this, I of course need an array of pixels, and since the screen in QuickBasic can be a maximum of 640x480 that is how big I want the array to be. So I declare it like this.

TYPE Image
Pixel(0 TO 319, 0 TO 479) AS INTEGER
Palette AS ColourPalette
END TYPE

I get a "Subscript out of range" and I know that this is due to the limit of an array being 64 K. But is there a way I can work around that limit, and still have as big an array as I have above?

If I instead make the array much smaller, it works. But I get the error "Type larger than 65535 bytes" due to the Palette line.

TYPE Image
Pixel(0 TO 159, 0 TO 198) AS INTEGER
Palette AS ColourPalette
END TYPE

My ColourPalette is another user-defined type, consisting of 4 integers. I assume that it is connected to the above error, and can perhaps be resolved in the same way.

If I use REM in front of the Palette statement, I can run my program. But when I try to compile it with BC, I get a "Data-memory overflow" in the following line.

CALL ShowImage(ImageArray(UBOUND(ImageArray, 1)), XCoordinate, YCoordinate)

ImageArray is simply an array of the type Image, that I defined above. It only has one element though, ImageArray(0) so far.

Yes, I'm sure that many of you will look at this and cringe at the thought of all the mistakes I'm sure I have done. That is fine. But please keep in mind that besides making something I find useful, this is to me also a process of learning. So I'll happily take advice and opinions about changes.

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Thu Aug 23, 2007 8:14 pm

An array in a user defined type, of this size, isn't always a good idea...when you think about it: 320x480x4 (4 bytes for an integer variable) is about 613Kb in size. That's alot for QB to handle. It depends on what exactly you want to do with that user defined type.

My first suggestion would be to create an array as a dynamic array with the following:

'$DYNAMIC
DIM MyImage() AS INTEGER

Then to initialize this you need to redim it like so:

REDIM MyImage(319, 479) AS INTEGER

This will create the array in heap space instead of in the data section which is indeed limited to 64Kb as you said. If this still doesn't work (is still too big to be executed). read on :-)

If the goal is to be able to load and display an image, I suggest you look up the BLOAD and BSAVE statements in the online help. They might do what you need without the use of a user defined type.

IF that's not the only case, just describe in a bit more details what you wan to to do with the user defined type. And from that description we can push you in the right direction. :-)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

ThemePark
Coder
Posts: 17
Joined: Wed Aug 22, 2007 6:37 pm

Post by ThemePark » Sat Aug 25, 2007 7:47 pm

While I haven't tried compiling it yet, and thus don't know if it has resolved the Data Memory issue, it seems to work for the other issues. Thank you.

I must admit though that I'd prefer if there was a solution that allowed me to keep my userdefined types, mainly for good structure.

At the moment I'm trying to build a graphics library for myself that I can then use in all my future QuickBasic games. One of the things I had thought of that would be useful, would be to load and save images. Not really much else to the whole image thing right about now. But I have far from completed the list of things I want to implement into the program, so I might come up with further image manipulation subs or functions that I'd like to implement. Or I might not, hard to say obviously.

But if there really isn't a good solution for this that allows me to keep my Image type, then no biggie. I can always make the array like you said, and make a Palette array as well. But I would prefer the user defined type.

Also, isn't an Integer 2 bytes? If not, I could cut my first dimension in the array in half, since that's what I did in the first place, to save space and because Bytes as a data type don't exist in QuickBasic.

EDIT: Well I just tried your suggestion out again and now it doesn't work. But also I'm now allowed to make an empty array, it seems. If I do I get an "Expected: expression" error, and in all other cases I get the same "Subscript out of range" error as before.

Post Reply