Allow datasection with fixed-length types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Allow datasection with fixed-length types

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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?
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Maybe Mistrel hasn't noticed that DataSections are write-only? Apart from that I don't know.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Trond wrote:...DataSections are write-only?
I don't quite understand this part, could you explain?
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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?
cxAlex
User
User
Posts: 88
Joined: Fri Oct 24, 2008 11:29 pm
Location: Austria
Contact:

Post 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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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..
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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)
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

So dim it at 0,0 then repoint it and redim.
BERESHEIT
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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?
Post Reply