This seems strange...
CannibalRobotics
Posts: 535
Is this weird or a bad assumption.
I have several veriables defined as BYTEs. in the VAR section. One of them is adjacent a string as follows.
VAR
BYTE Count
BYTE StringName[15]
.
Count goes from 0 to 14 so to save space I dimensioned it as a BYTE.
The string was misbehaving, whenever I wrote something to it then went back to read it I got garbage or nothing. After considerable frustration I fixed the problem by the defining count as a LONG. Apparently when the statement Count :=0 was executed the first byte of StringName became 0 too. Thus reading as an empty string to 0-terminated string operations.
So I pose the question, if you set a BYTE to 0 with code "Count := 0" does it assume 'Count' is an integer and set 4 bytes to 0 starting at the memory address of 'Count'?
Is this true of other integer operators? If I bump a BYTE with ++ is it actually incrementing the LSB BYTE 3 doors down?
Seems odd but I have no other explanation?
I have several veriables defined as BYTEs. in the VAR section. One of them is adjacent a string as follows.
VAR
BYTE Count
BYTE StringName[15]
.
Count goes from 0 to 14 so to save space I dimensioned it as a BYTE.
The string was misbehaving, whenever I wrote something to it then went back to read it I got garbage or nothing. After considerable frustration I fixed the problem by the defining count as a LONG. Apparently when the statement Count :=0 was executed the first byte of StringName became 0 too. Thus reading as an empty string to 0-terminated string operations.
So I pose the question, if you set a BYTE to 0 with code "Count := 0" does it assume 'Count' is an integer and set 4 bytes to 0 starting at the memory address of 'Count'?
Is this true of other integer operators? If I bump a BYTE with ++ is it actually incrementing the LSB BYTE 3 doors down?
Seems odd but I have no other explanation?
Comments
produced this output
You should post your code to let us try to find an error.
John Abshier
-Phil
I agree with John. You should post your code to see if we can come up with a different explanation.
There are some operations that do strange things if you use a byte instead of a long but setting the variable equal to zero isn't one of them.
If there's a chance the number can be negative, IMO, it's a good idea to use a long. I also use longs for numbers that will have bitwise shifts and rotations.
Some of your post is a bit cryptic. You mention "memory address" but a byte isn't large enough to hold most memory addresses. It could hold the array index number though.
Again, the mystery will probably be resolved with your posting the code.
@Phil, per the length of the string, the count goes from 0 to 14 which would be the 15 slots allocated? This is actually padded by 3 extra bytes just for good measure since I know all of the possible strings content and the longest one, including terminator 0 is 11. Anyway, the problem is at the front end of the string.
The anomoly was also solved by moving the 'BYTE Count' decleration to another position in the line up of the VAR definitions, weird?
@John, I know, it should not have behaved this way and in a vacuum, that piece of code does the same thing on my props. But when changing the type from BYTE to LONG or Moving the BYTE definition stops the changing of adjacent content, makes one go 'hum'. What I posted below is a more accurate representation of the situation.
@Duane, when I used the term 'memory address' I was talking about the physical address of 'Count' not the address in 'Count' - bytes too small for that.
So for now, the behavior is eliminated but the cause is still a bit of a mistery....
Thanks all.
VAR sections are reordered on compile. Long are grouped with longs, bytes with bytes. Changing Count to a long moved the variable in relationship to StringName. I'm sure the bug still exists it's just better masked.
I've had this situation described in post #1. What helped me solve the problem is accepting the horrible truth... I write bugs from time to time.