Page 1 of 1
Can't read bytes in data section
Posted: Sun May 08, 2011 2:51 pm
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
Re: Can't read bytes in data section
Posted: Sun May 08, 2011 2:56 pm
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
Re: Can't read bytes in data section
Posted: Sun May 08, 2011 3:10 pm
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.
Re: Can't read bytes in data section
Posted: Sun May 08, 2011 3:31 pm
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
Re: Can't read bytes in data section
Posted: Sun May 08, 2011 3:32 pm
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).
Re: Can't read bytes in data section
Posted: Mon May 09, 2011 10:47 am
by Fred
Mandatory type looks like a good idea to avoid such trap.
Re: Can't read bytes in data section
Posted: Mon May 09, 2011 8:08 pm
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?
Re: Can't read bytes in data section
Posted: Tue May 10, 2011 9:05 am
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).
Re: Can't read bytes in data section
Posted: Tue May 10, 2011 9:44 am
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!