Page 1 of 2
Allow datasection with fixed-length types
Posted: Wed Jul 01, 2009 2:21 am
by Mistrel
This is just a rudimentary example to demonstrate what I'm talking about:
Code: Select all
DataSection
Object:
Data.l[3] ;/ x, y, z
Data.l[2] ;/ max length
Data.l[5] ;/ local object data
Object_string:
Data.s[16],[26],[3] ;/ object name, description, 3-char code
EndDataSection
It would also be nice to be able to use structures here as well.
Posted: Wed Jul 01, 2009 11:46 am
by Kaeru Gaman
the length of the Datasection is clearly defined by the length of the Data you put into it.
what would be the use of defining it twice?
Posted: Wed Jul 01, 2009 8:27 pm
by Trond
Maybe Mistrel hasn't noticed that DataSections are write-only? Apart from that I don't know.
Posted: Wed Jul 01, 2009 8:46 pm
by netmaestro
Trond wrote:...DataSections are write-only?
I don't quite understand this part, could you explain?
Posted: Wed Jul 01, 2009 8:49 pm
by Kaeru Gaman
DataSections are not read only... you can read and write aswell.
but the DataSection gets a fixed size by the values you put in.
Code: Select all
Data.b 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
creates a Section of 16 byte length
Code: Select all
Data.l 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
creates a Section of 64 byte length
... so, I don't understand what he is requesting.
Posted: Wed Jul 01, 2009 8:50 pm
by #NULL
for numbers it were usefull then if you could define less elements of such an data block, not having to pad the remaining elements with 0s. and for strings it would be usefull because your string elements would then have all the same length regardless how long the literals are:
Code: Select all
Data.s [16],[16] "abc", "qrstuvw" ; will be of same size
and for numbers and strings as well you can define offsets between your data easier, without having to pad 0s or ""s.
@trond
what about writing to datasection indirectly, by modifying an *.exe by another one for example?
Posted: Wed Jul 01, 2009 8:51 pm
by cxAlex
Trond wrote:Maybe Mistrel hasn't noticed that DataSections are write-only? Apart from that I don't know.
DataSections are writeable:
Code: Select all
Define *Val.Long, Val.l
*Val = ?MyData
Restore MyData
Read.l Val
Debug Val
; Now change the Data in the DataSection
*Val\l = 123
Restore MyData
Read.l Val
Debug Val
DataSection
MyData:
Data.l 10
EndDataSection
Posted: Wed Jul 01, 2009 9:56 pm
by Psychophanta
Not sure, but i think datasections are read only sections, as it is part of the program body and it should not be writable.
The above example, which can be easely like this, is not safe, is it?
Code: Select all
PokeL(?MyData,123)
Read.l d.l
Debug d
DataSection
MyData:Data.l 10
Maybe Trond wanted to say read-only.
Posted: Wed Jul 01, 2009 11:00 pm
by Demivec
Psychophanta wrote:Not sure, but i think datasections are read only sections, as it is part of the program body and it should not be writable.
The above example, which can be easely like this, is not safe, is it?
It is safe (in the right hands).
You can also change the code in the program body as well. This allows for self-modifying code.
It is normally best to avoid both practices for the sake of "safety". What is best though, depends on the circumstances and the skill of the programmer.
Posted: Thu Jul 02, 2009 12:29 am
by Kaeru Gaman
btw... It would be nice to have the possibility to assing a fully dimensionated Array to a DataSection.
I mean, Dim Array( #X, #Y ), and then bow the pointer to a DataSection
to use the Space in the Section with predefined values and don't waste space by allocating twice..
Posted: Thu Jul 02, 2009 1:16 am
by netmaestro
@KG:
Code: Select all
DataSection
lbl:
Data.l 10,20,30,40,50,60
EndDataSection
Dim stuff.l(2,1)
stuff()=?lbl
Debug stuff(0,0)
stuff(0,0)=99
Debug stuff(0,0)
Posted: Thu Jul 02, 2009 3:28 am
by Kaeru Gaman
@netmaestro
butI think there still is the problem with the previous allocated space...
the old location of the Array isn't freed when I just bow the pointer that way, is it?
so, when I have a 512x512 Long-Array, I would waste (leak) a full MB of Memory...
Posted: Thu Jul 02, 2009 3:31 am
by netmaestro
So dim it at 0,0 then repoint it and redim.
Posted: Thu Jul 02, 2009 8:19 am
by helpy
Kaeru Gaman wrote:btw... It would be nice to have the possibility to assing a fully dimensionated Array to a DataSection.
I mean, Dim Array( #X, #Y ), and then bow the pointer to a DataSection
to use the Space in the Section with predefined values and don't waste space by allocating twice..
Do you mean something like this:
Code: Select all
Structure MyArrayRow
item.i[10]
EndStructure
Structure MyArray
row.MyArrayRow[2]
EndStructure
*myArray.MyArray = ?MyArray
For row = 0 To 1
For item = 0 To 9
Debug *myArray\row[row]\item[item]
Next
Next
; A bit easier to write with a macro
Macro ArrayItem(xArray,x,y)
xArray\row[x]\item[y]
EndMacro
For row = 0 To 1
For item = 0 To 9
Debug ArrayItem(*myArray,row,item)
Next
Next
DataSection
MyArray:
Data.i 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Data.i 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
EndDataSection
cu, helpy
Posted: Thu Jul 02, 2009 8:59 am
by Trond
I meant to say read-only.
But I do not understand why Fred would make DataSections writeable, as it means they are basically useless. If it was read-only the same datasection would be shared amongst all loaded copies of the executable. If it is writable, it is no different from the rest of the program, so why make it a separate section?