Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / DIM with negative value

Author
Message
xsankyo
14
Years of Service
User Offline
Joined: 13th May 2010
Location: Italy
Posted: 16th Jun 2010 23:29 Edited at: 18th Jun 2010 13:45
Hi everybody

i create a simple bidimensional array



why DIM accepts negative coordinates ?
Zyronagon
16
Years of Service
User Offline
Joined: 6th Mar 2008
Location: USA
Posted: 17th Jun 2010 01:01
Well, this might be the blind leading the blind (I'm a noob), but here goes:

You're trying to print something that isn't there. You told the program "for a=0 to 39" and now you're trying to print -15. The only numbers in the for-next loop are 0-39, so -15 isn't on the list.
xsankyo
14
Years of Service
User Offline
Joined: 13th May 2010
Location: Italy
Posted: 17th Jun 2010 13:13
hi, sorry for my bad english.

Yes it is true, but if you run the code work well and
the statement "print ex(-15,39)" print a number.

Why ?

If you try to use " print ex(15,-15)" with the second term negative, the code produce an error.
luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 17th Jun 2010 21:47
I think this is some bug because nobody is expecting someone to try making negative array.Making array is like filling a cup with water, the cup is represented with the memory you have, more memory = bigger cup, trying to make negative array is like trying to add more emptyness to the empty cup.Why you need negative array anyway?

Where there is a will, there is a way.
I often edit my posts, that`s who i am
xsankyo
14
Years of Service
User Offline
Joined: 13th May 2010
Location: Italy
Posted: 17th Jun 2010 22:17
I do not need negative array, during the writing of a program I misspelled some numbers and found this strange.

I just wanted to know if is a bug or if there is some other reason.
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 17th Jun 2010 23:24
I would assume that the position in the dynamic memory, allocated by the program is calculated as: Y * Width + X

eg. dim Arr(Width, Height)
then position X, Y would be at (Y * Width + X) * TypeSize

This means that:
ex(-15,39)
is at 39 * 40 + (-15) = 1 545
which is a valid position in the dynamic memory.

This is my own assumption though, I have no base for this.

Cheers!
Sven B

luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 18th Jun 2010 01:32
Strangely trying to make some negative array it says that there is not enought memory but if i do dim my_array(-1) it does it without error.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 18th Jun 2010 01:46
Quote: "Strangely trying to make some negative array it says that there is not enought memory"


It's probably treating the size as an unsigned number, which means it thinks you want to create a very large array.

Quote: "but if i do dim my_array(-1) it does it without error."


This is valid for creating an array with no elements.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 18th Jun 2010 22:06
Internally, DBPro arrays are single-dimensional. What DBPro does is take your indexes, calculate where they refer to as a offset into that array based on the dimensions that you originally provided when dimming the array, test to see if the offset is valid for the array, and then either fetch the value stored there, or raise an error.

If you know the details of how this works then it's relatively easy to predict what happens:


... and no, I don't think it's the right way to handle array bounds checking.

Login to post a reply

Server time is: 2024-11-23 14:18:04
Your offset time is: 2024-11-23 14:18:04