QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: NOVARSEG on February 13, 2021, 12:46:12 am
-
Sidetracking a bit.
I want to devise some test code to see if the order of data items in TYPE structures are important.
TYPE ABC
Z AS INTEGER
Y AS INTEGER
END TYPE
DIM b AS _OFFSET
DIM c AS ABC
c.Z = 100
c.Y = 1000
b = _OFFSET(c)
so the memory address of TYPE ABC is stored in b. Now how do you read the entire 4 bytes in that TYPE into an LONG , using b ?
-
@NOVARSEG
Check the Wiki for MEM
-
OK yes MEM . It looks like the order is important when referencing TYPE data structures by address but want to make sure with some tests
I was reading on some site that an option to padding was rearranging the items in a data structure and thought how is that?
-
@NOVARSEG
That would only be an option when it is a type that you are making for your own functions. For anything like WinAPI, you can't reorder the variables unless you WANT the data to go to the wrong variables
-
@SpriggsySpriggs
Makes sense thanks
DIM L AS LONG
DIM m AS _MEM
_MEMGET m, m.OFFSET, B
_MEMPUT m, m.OFFSET, L
PRINT L
get a memory not initialized error.
-
@SpriggsySpriggs
Makes sense thanks
get a memory not initialized error.
Makes sense, as you haven’t initialized your memory. It points at nothing.
Here’s what you’d be doing with file access:
m = FREEFILE
GET m, 0, B
And, you’d get an error then too, as you’ve never actually opened any file on your drive to access. Start with wiki examples, or my tutorials, and learn the basic syntax of the commands, before just shooting off blindly with them.
What you’re doing is almost as bad as trying to PRINT HELLO WORLD without using quotes, and then complaining when it doesn’t work as you imagine.
-
Ok IM totally stupid when it comes to mem
DIM m AS _MEM
DIM N AS STRING * 4
N = "abcd"
m = _MEM(N)
_MEMPUT m, m.OFFSET, N
N = ""
_MEMGET m, m.OFFSET, N
PRINT N
does not print abcd
-
Of course it's not going to print abcd. You just reset N to "".
Start a topic on mem, and ask questions there about it. I don't have a clue what you're trying to do, but it's definitely not related to the topic at hand.
-
yes off topic did not want to start a new thread
Will get back on track but MEM to me is an abstraction on so many levels
I thought N is now stored in m with
_MEMPUT m, m.OFFSET, N
N = ""
then do the reverse
_MEMGET m, m.OFFSET, N
see how I don't understand MEM?. At least I know what I don't understand
PRINT N
****
In C there is
The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator.
Ok so i got the address of TYPE ABC structure in B
TYPE ABC
Z AS INTEGER
Y AS INTEGER
END TYPE
DIM b AS _OFFSET
DIM c AS ABC
c.Z = 100
c.Y = 1000
b = _OFFSET(c)
In C there is the "value of operator."
Do I really need MEM stuff to do a "value of operator" on B"
-
@NOVARSEG
Like Steve said, you might want to go ahead and make a new thread and discuss MEM there. You might find more help there since the title will at least match the topic being discussed. People who understand MEM will see your question and respond.
-
If I do that then more people will see how stupid I am when it comes to MEM
-
If I do that then more people will see how stupid I am when it comes to MEM
Not stupid, just blind seeing the obvious. you think your m is another variable, which you can exchange the value of N with and wise versa, but NO, m is not another variable, it's just ANOTHER WAY TO ACCESS your variable N.
Ie. m is a pseudonym for N, doesn't matter if you access N directly (N="") or via _MEMGET/PUT, you always change N and only N.
m is a pointer to the memory, where the value of N is stored, not more, not less.
EDIT:
If you're likely more familiar with C/C++, it's like taking the reference of a variable here:
char N;
char *m = &N;
-
I have no idea how/why this works. Finally got it to print out.
Z is LSW, Y is MSW. Could someone explain how it works
with
c.Z = 400
c.Y = 0
print N& = 400
****
with
c.Z = 1
c.Y = 1
print N& = 65537
-
_MEMPUT does the reverse of that
N = 2 ^ 32 - 1
C.Y = 0
C.Z = 0
print C.Z = -1
print C.Y = -1
****
This proves that variables in a TYPE list, are contiguous in memory
-
What happens when a variable length string is added to the TYPE list
C.S = "abcdefghijk"
N = 2 ^ 32 - 1
C.Y = 0
C.Z = 0
print C.Z = -1
print C.Y = -1
The "S AS STRING" takes 4 bytes in the TYPE list, but there are 11 bytes stored in S
Now when the variable length string is changed to a fixed length string like
'print len(c)
C.S = "abcdefghijk"
N = 2 ^ 32 - 1
C.Y = 0
C.Z = 0
print C.Z = -1
print C.Y = -1
Then "S AS STRING * 11" takes up 11 bytes in the TYPE list. So we know that "abcdefghijk" is stored in memory just before Z and Y integers. That is not the case with the variable length string.
-
That is, because fixed length strings are directly incorporated into your type, hence 11 bytes, but for variable length strings only a pointer to the string descriptor is incorporated into the type, which should be 4 bytes when using the 32bit version of QB64, while it should be 8 bytes when using the 64bit version of QB64.
In different words, fixed length is incorporated as is, variable length strings are just incorporated AS _OFFSET.
-
As RhoSigma suggested , the first 4 bytes in the TYPE list is the memory address of variable length STRING S
PRINT N = memory address of variable length string.
-
PRINT N = memory address of variable length string.
This may, or may not, be true. Variable length strings can move around in memory at any time, depending on when your OS does clean up calls and memory packing/rearranging. When you got the value of N, that's where your variable length string was at that moment, but it could be somewhere else by the time you get to, and complete, the PRINT statement. It's this movement of variable length strings which is why they don't work with _MEM as it currently exists.