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.

DarkBASIC Discussion / Trouble with setting size of memblock...

Author
Message
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Jul 2009 21:20
Hello

I am going to give memblocks a try. This is the first time I have really used them. So what I want to do is write variables to a memblock instead of using arrays, because if I set up an array in a function, it is local to that function and I can't use it anywhere else. But with memblocks I can access the data anywhere in the program.

So here is the problem.

I am planning to save all variables and arrays that have to do with the waypoints in the memblock. Before the user can start using the waypoint commands, he has to call the "Enable_waypoint_commands()" command to create the memblock and various other bits the functions need. How am I going to determine the size of this memblock?

I was thinking of extending it when it is too small, but I would have to read in all variables, delete the memblock, create a new, larger memblock (What size should I make that one?) and write everything into it.

Is there an easier way?

TheComet


Make the path of your enemies easier with Waypoint Pro!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 24th Jul 2009 23:00
A real easy way is to make the memblock ridiculously larger than you'd ever need it, but that's just a waste of memory.

If you are using other variables and copying them to a memblock, each variable type (except strings) are 4 bytes long. A memblocks size is in bytes. So for every integer or float, you need to increase the size of the memblock by 4 bytes. If it is an array, then you need to increase the memblock
4*((index+1)*(index+1)*(index+1) etc) for each dimension - this array would be a 3 dimensional array: myarr(a,b,c) so if the array was dimensioned as myarr(5,6,3) then the size would be
4*((5+1)*(6+1)*(3+1))

So just keep adding those variables up and that is the minimum size you need to make the memblock. For each string, you can reserve 255 bytes if you like so there's flecibility or you can just use the length of the string - in whcih case you'd have to reallocate the memblock every time the string changed.

The way to manage dynamic allocation (resizing the memblock) is to create two memblocks of equal size. Copy the main memblock into the second memblock, delete the main memblock, resize it to accomodate the new information, then either copy back the information from the backup memblock (which is why I said create two) or rewrite the information from the variables into the resized memblock. Then you can delete the 2nd memblock if you created it.

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 24th Jul 2009 23:12
Quote: "each variable type (except strings) are 4 bytes "


unless the numbers are large could he use bytes? if the numbers are small and integers you could them in bytes that would take less space.

you can just create a thing to check the size of the number and determine weather or not you need a integer float or byte. just to be therough lol

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 24th Jul 2009 23:33
Yeah. That opens up a discussion on data types. A negative value, for example, has to be 4 bytes long and written to a memblock as a float; even if it is an integer value (I should have mentioned that earlier). So while the data type sizes could be changed, it takes a bit more planning. It's much easier, in my opinion, to size the memblock at 4 bytes per variable if you are writing DBC variables over to the memblock; but it's more efficient to size the memblock according to how much memory you actually need as Caleb1994 is suggesting.

Enjoy your day.
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 25th Jul 2009 01:06 Edited at: 25th Jul 2009 01:08
I did this once. I just basically use the memblock as a massive array.

If I dim a normal array it would be like this:
dim numbers(8,9,6)

If I made a memblock array out of those numbers it would be like this:
make memblock memnum,896

The first position 8 would be 800. The second position 9 would be 90. The third position 6 would be 6.

So If I were trying to get the last variable in the array.

In a normal array it would be:
number(8,9,6)

In memblock array it would be:
memblock byte(memnum,896)

If you were trying to use dword or something then you would just multiply it by how many bytes it would take up.

So if I made the array memblock 896 out of dwords. The size would really be 3584 and I would multiply the position I'm trying to get to by 4.

So in the dword memblock array the final variable position would be found like this:

memblock dword(memnum,(8*4)+(9*4)+(6*4))

Thats how I would do it if I were trying to use 1 memblock.
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Jul 2009 01:18
what size are strings? arent they like a byte per character?

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2009 02:02 Edited at: 25th Jul 2009 02:02
Yep. They are 8 bits large. Think of the asc() command. It will return a value between 1 and 256, if I remember correctly. There are 256 combinations 8 bits can do:



You can easily decode binary code into characters.

Let's say you want to decode this character:

01100001

Here's the diagram:

0 - 128
1 - 64
1 - 32
0 - 16
0 - 8
0 - 4
0 - 2
1 - 1

01100001=0+64+32+0+0+0+0+1=97
string$=chr$(97)
print string$

If I remember correctly, that should be an "a".

TheComet


Make the path of your enemies easier with Waypoint Pro!
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 25th Jul 2009 02:11
Or use the scancode of the letter. That seems like it would be a lot easier. Also, my new function would come in handy . By giving you the letter of the scancode when converting it back to string. The function is on this page, just in case you want it.

http://forum.thegamecreators.com/?m=forum_view&t=154818&b=10
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 25th Jul 2009 07:24
Quote: "There are 256 combinations 8 bits can do:
"


i am pretty sure that it's 255. the highest value for a byte is 255, plus:

1+2+4+8+16+32+64+128=255

lol

SmartGuy:

Quote: "what size are strings? arent they like a byte per character?"


if you think about the word "String" it tells you. it's a string of characters. but what are characters? they are numbers so prety much just take all the characters from a string, get the ascii values and store them in a "String" of variables in the memblock.

just in case you ever use WinAPI if you need a pointer to a string, use that lol just store it in a memblock and send the pointer to the memblock.

New Site! Check it out \/
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2009 11:00
Quote: "i am pretty sure that it's 255. the highest value for a byte is 255, plus:"


I know what you mean. But it has 256 combinations, because the lowest value is:

00000000=0+0+0+0+0+0+0+0=0

highest value is:

11111111=128+64+32+16+8+4+2+1=255

0 to 255 is 256 combinations. 0 counts as one too.

TheComet


Make the path of your enemies easier with Waypoint Pro!
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 25th Jul 2009 14:46
Quote: "0 to 255 is 256 combinations. 0 counts as one too."


Oh Whoops i see what you mean. i forgot about zero lol.

New Site! Check it out \/
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2009 16:22
Thanks everyone for your help!

If I were to save 4 3-dimensional arrays to one memblock, would this be correct for finding the positions?



TheComet


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Jul 2009 19:20
wow, a byte per character * 255 characters max in a strin, thats a lot of data compared to the other data types

WOW!! i didnt know scancode had anything to do with asc() values, I thought it was just a random sequence of numbers!!! DDDDUHHHHHH!!!

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2009 19:32
The question above SmartGuy's post still remains, and this one: A dword is a value between 0 and 2147483648 and consists of 4 bytes, but a float is a value between -2147483648 and 2147483648 and still consists of four bytes... Shouldn't it be 8 bytes?

TheComet


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Jul 2009 19:41
if ur calculation of what 4 bytes is is correct as is your float range then yes that should be 8 bytes, ill do a little research on it

That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Jul 2009 19:47
well according to this:



its 4 bytes, but what is that percent sign at the start? maybe that has to do with that fact that its showing it in binary?

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jul 2009 20:36 Edited at: 25th Jul 2009 22:39
Quote: "i didnt know scancode had anything to do with asc() values"

It doesn't. They are two different sets of values.

Quote: "A dword is a value between 0 and 2147483648 and consists of 4 bytes, but a float is a value between -2147483648 and 2147483648 and still consists of four bytes... Shouldn't it be 8 bytes?"

Alright, here comes the data type explanation:

A data type determines what bit configuration a number is stored and referenced by. No mystery there. DBC is 32 bit. That means the most bits any number will have is 32 or 4 bytes.

The standard variables in DBC are integer and float.
The DBC integer has what is called a signed long data type. That means it has a negative and a positive range of values. Since it is a 32 bit value, a negetive value is determined by setting the highest bit (bit 31) to 1.

A 32 bit number (ignoring the sign) has a maximum value of 4294967295. But because DBC's integer type is SIGNED (when bit 31 is 1), the number is divided between a - and + range within 4294967295 values: -2147483648 and 2147483647 .

When it comes to memblocks, there are 4 data types:
BYTE
WORD
DWORD
FLOAT

The first three are UNSIGNED values. That means they range from 0 to their maximum bit values - EXCEPT in the case of a DWORD.

I'll have to finish the rest later, I gotta get something to eat.

[EDIT] delicious!

Back to work....

Quote: "EXCEPT in the case of a DWORD"

A DWORD will only read or write up to 2147483647. If you need to use negative values, then use a FLOAT data type for a memblock.

A FLOAT data type uses similar rules as the DBC integer data type except that there is an algorithm used to determine the value with a decimal point. The decimal point "floats", i.e. it can change position depending on the value of the number. So in most cases, a FLOAT is not interchangeable with a DWORD in terms of reading or writing either to a memblock. However, you have no choice but to write a float value if you want it to be negative, or create a 2's compliment function so that you can write other data types as negative values. If you write a FLOAT, you have to read it back as a FLOAT.

The size of the data types for DBC:
Memblocks
BYTE = 1 byte = 8 bits UNSIGNED
WORD = 2 bytes = 16 bits UNSIGNED
DWORD = 4 bytes = 32 bits UNSIGNED
FLOAT = 4 bytes = 32 bits SIGNED
STRING = BYTE array[] = 8 bits per byte UNSIGNED

Variables
Integer = 4 bytes = 32 bits SIGNED LONG
Float # = 4 bytes = 32 bits = SINGLE real
String $ = 1 byte per char = max 255 bytes

File Data Types
LONG = 4 bytes = 32 bits SIGNED LONG
WORD = 2 bytes = 16 bits UNSIGNED int
FLOAT = 4 bytes = 32 bits SINGLE real
BYTE = 1 byte = 8 bits UNSIGNED char
STRING = BYTE array[] = 8 bits per byte UNSIGNED (max 255 length including CHR$(10) CHR$(13) at end)

UNSIGNED means the value starts at 0 and goes up to it's max value based on the number of bits in the data type.

SIGNED means the max value is roughly divided by 2 with the lower set of numbers being negative values and the upper set of numbers being positive.

STRING data types as arrays means individual bytes representing character values are strung together in sequence. In a memblock, this is only limited by memory. It's also unlimited in a file unless you use the built in WRITE or READ STRING commands, then the string is limited to 255 bytes strung together - because it is converted to a DBC string variable type.

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2009 20:41 Edited at: 25th Jul 2009 20:49
Quote: "I'll have to finish the rest later, I gotta get something to eat."


Must be the funniest thing I have ever heard from you! Enjoy your meal

I think I am starting to understand, thanks a lot for your efforts!

TheComet

EDIT: I can not figure this out. Here is the test code I'm using(I know, very messy):



What I am doing is giving four 3-dimensional arrays random numbers, and writing the same info to a memblock, so the four arrays are in the memblock. But when I read them again they are not the same at all. When you see them print to the screen, the two numbers separated by a comma have to be the same. But they aren't. Am I writing them incorrectly?


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Jul 2009 20:42
Quote: "I'll have to finish the rest later, I gotta get something to eat"

this scares me, ive never seen latch quit in the middle of an explanation before

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 26th Jul 2009 02:00
@TheComet

Please try a 1 or 2 dimensional array in a memblock so you can get a better feel of what you are trying to do and, I can't understand what you are trying to do with the code above. I don't know why you are sizing the memblocks like you are.

Though the size of memblock in terms of bytes for a multidimensional array is
(byte size)*((index1+1)*(index2+1) etc.))

Accessing individual indeces isn't as straight forward. For example an array with a 4 byte data type and indeces of 1,0,1 position would = 64 but so would 0,1,1 .

That suggests that you cannot find the position with the same formula. You'll have to use that noggin a bit to figure it out!

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 27th Jul 2009 05:23 Edited at: 27th Jul 2009 05:28
the formula shouldn't be hard. havn't tested it but if you are trying to find this value

Arr(1, 3)

and the max is

Arr(3,3)

with 4 byte data type. it should be:

Pos = (2 * 4 + 3) * 4

3 * 4 - 1 because you need to get to the last position on the 2nd(counting 0) space in the first index. if that makes sense here it is another way

Pos = (how far into the first you need* size of second dim + How many into that space you need) * Variable Byte size

so if you needed

arr(2, 1)

Position = (3 * 4 + 1) * 4

i think thats right i havn't tested it though

New Site! Check it out \/
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 27th Jul 2009 21:56
Thanks everyone for your help!

@ Caleb1994

Thanks for that, but unfortunately I can't use it, it's a little late... What I did is this formula:



The code won't work if you compile it as it is...

TheComet


Make the path of your enemies easier with Waypoint Pro!
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 29th Jul 2009 19:25
Ok sweet, HOpe you have fun now that you figured it out lol

Quote: "The code won't work if you compile it as it is..."
'

Ya i noticed

New Site! Check it out \/

Login to post a reply

Server time is: 2024-05-20 04:55:44
Your offset time is: 2024-05-20 04:55:44