Can't read bytes in data section

Just starting out? Need help? Post your questions and find answers here.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Can't read bytes in data section

Post by MachineCode »

Can someone explain to me why this doesn't work, when it used to in an older version of PureBasic? The error message given is "Read data error: no more data" but I'm not trying to read more data than exists. WTF?

The manual states that the "byte" data type holds values from -128 to +127, and "word" from -32768 to +32767, but if I use anything less than "long" type, the loop doesn't read all the values. I don't understand why, as all the values in the data section are under +127.

Code: Select all

For num=1 To 15
  Read value.b
  Debug value
Next

DataSection
  Data.b 77,101,114,114,121,32,67,104,114,105,115,116,109,97,115
EndDataSection
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Can't read bytes in data section

Post by Comtois »

since 4.30
- Changed: 'Read' keyword now requiers a type (Read.l, Read.q etc.) to avoid 64 bits migration problems

Code: Select all

For num=1 To 15
  Read.b value.b
  Debug value
Next

DataSection
  Data.b 77,101,114,114,121,32,67,104,114,105,115,116,109,97,115
EndDataSection
Please correct my english
http://purebasic.developpez.com/
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Can't read bytes in data section

Post by MachineCode »

Thanks for that. The manual needs updating, then. Because for the "Read" keyword, it currently shows [ and ] brackets, which means specifying a type is optional, and it says the default type (integer) is used if not specified. See:

Code: Select all

Read[.<type>] <variable>
The type of data to read is determined by the type suffix. The default type will be used if it is not specified.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Can't read bytes in data section

Post by ts-soft »

The manual is correct, see:

Code: Select all

For num=1 To 4
  Read value.b
  Debug value
Next

DataSection
  Data.b 77,101,114,114,121,32,67,104,114,105,115,116,109,97,115
EndDataSection 
Reads all values as long in x86
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Can't read bytes in data section

Post by tinman »

MachineCode wrote:Thanks for that. The manual needs updating, then. Because for the "Read" keyword, it currently shows [ and ] brackets, which means specifying a type is optional, and it says the default type (integer) is used if not specified. See:

Code: Select all

Read[.<type>] <variable>
The type of data to read is determined by the type suffix. The default type will be used if it is not specified.
Without the type specifier it reads an integer from memory, converts the integer to your byte variable and then increments by the number of bytes in an integer. Increasing by 4 or 8 bytes each time eventually runs off the end of your data section.

So as far as the syntax of the command is concerned the type is optional and the manual is currently syntactically correct, which is maybe misleading or unclear especially when the behaviour used to be different. If a change it required it would be required to the command, not just the manual.

Think about it in terms of peeking. The size of memory being read from depends on the command, not where the result is stored. You wouldn't do this and expect only a byte to be accessed in memory:

value.b = PeekI(?label)

So consider the Read command in the same way, you need to tell it what size to read using the suffix or it will default to integer. Perhaps the best option would be to make the type specifier mandatory (for example, you do not get a Peek command with an optional type).
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Can't read bytes in data section

Post by Fred »

Mandatory type looks like a good idea to avoid such trap.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Can't read bytes in data section

Post by rsts »

Fred wrote:Mandatory type looks like a good idea to avoid such trap.
Will this break existing code without a specified type?

If so, maybe just a clarification in the documentation?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Can't read bytes in data section

Post by Fred »

It will probably break, but actually a code without suffix will break on 64 bit (as the default type will change). I don't think it's a reliable solution for the future (if i miss something, don't hesitate to tell).
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Can't read bytes in data section

Post by MachineCode »

Fred wrote:It will probably break
Don't worry about that... it's just a simple search/replace to fix our sources. Go for it!
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply