MEM: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by 2 users not shown)
Line 10: Line 10:
''Variable TYPE:''
''Variable TYPE:''
* Memory DOT values are actually part of the built in memory variable [[Variable Types|type]] in QB64. The following [[TYPE]] is built in:
* Memory DOT values are actually part of the built in memory variable [[Variable Types|type]] in QB64. The following [[TYPE]] is built in:
{{WhiteStart}}
{{FixedStart}}
TYPE memory_type
TYPE memory_type
   OFFSET AS _OFFSET      'start location of block(changes with byte position)
   OFFSET AS _OFFSET      'start location of block(changes with byte position)
Line 20: Line 20:
END TYPE
END TYPE


{{text|The above [[TYPE]] is for clarification purposes only. It '''doesn't need''' to be pasted in<br>a program to use _MEM.|red}}
{{Text|The above [[TYPE]] is for clarification purposes only. It '''doesn't need''' to be pasted in<br>a program to use _MEM.|red}}


{{text|'''IMPORTANT NOTE:''' ''As of Build 20170802/57 onward (early v1.2 development), mem.TYPE has<br>been changed to be an _OFFSET, just as mem.SIZE and mem.ELEMENTSIZE.''|red}}
{{Text|'''IMPORTANT NOTE:''' ''As of Build 20170802/57 onward (early v1.2 development), mem.TYPE has<br>been changed to be an _OFFSET, just as mem.SIZE and mem.ELEMENTSIZE.''|red}}
{{WhiteEnd}}
{{FixedEnd}}


===Usage===
=== Usage ===
* The _MEM type contains the following '''read-only''' elements where ''name'' is the _MEM variable name:
* The _MEM type contains the following '''read-only''' elements where ''name'' is the _MEM variable name:
:: ''name'''''.OFFSET''' is the current start position in the memory block AS [[_OFFSET]]. Add bytes to change position.
:: ''name'''''.OFFSET''' is the current start position in the memory block AS [[_OFFSET]]. Add bytes to change position.
Line 31: Line 31:
:: ''name'''''.TYPE''' is the type (represented as bits combined to form a value) AS [[_OFFSET]]:
:: ''name'''''.TYPE''' is the type (represented as bits combined to form a value) AS [[_OFFSET]]:


==.TYPE values (version 1.000 and up incl. all QB64-PE releases)==
 
== .TYPE values (version 1.000 and up incl. all QB64-PE releases) ==
:::* [bit 0] 1* byte types (_BYTE)
:::* [bit 0] 1* byte types (_BYTE)
:::* [bit 1] 2* byte types (INTEGER)
:::* [bit 1] 2* byte types (INTEGER)
Line 51: Line 52:
''Note: If a future integer, float or other type doesn't have a size that is 1,2,4,8,16,32,64,128 or 256 it won't have a size-bit set.''
''Note: If a future integer, float or other type doesn't have a size that is 1,2,4,8,16,32,64,128 or 256 it won't have a size-bit set.''


===Versions prior to 1.000 (never use with QB64-PE releases)===
=== Versions prior to 1.000 (never use with QB64-PE releases) ===
:::* 1 = Integer types such as [[_BYTE]], [[INTEGER]], [[LONG]], [[_INTEGER64]] or [[_OFFSET]]
:::* 1 = Integer types such as [[_BYTE]], [[INTEGER]], [[LONG]], [[_INTEGER64]] or [[_OFFSET]]
:::* 2 = [[_UNSIGNED]] variable types. Value must be added to the variable type value.(2 cannot be used by itself)
:::* 2 = [[_UNSIGNED]] variable types. Value must be added to the variable type value.(2 cannot be used by itself)
:::* 3 = ALL [[_UNSIGNED]] [[INTEGER]] type values.(add 1 + 2)
:::* 3 = ALL [[_UNSIGNED]] [[INTEGER]] type values.(add 1 + 2)
:::* 4 = Floating point types such as [[SINGLE]], [[DOUBLE]] or [[_FLOAT]]
:::* 4 = Floating point types such as [[SINGLE]], [[DOUBLE]] or [[_FLOAT]]
:::* 8 = [[STRING]]  
:::* 8 = [[STRING]]
:::* 0 = unknown(eg. created with [[_MEMNEW]]) or [[TYPE|user-defined-types]]
:::* 0 = unknown(eg. created with [[_MEMNEW]]) or [[TYPE|user-defined-types]]


Line 66: Line 67:
''Example 1:'' Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and [[_MEMEXISTS]] to check image has not been freed
''Example 1:'' Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and [[_MEMEXISTS]] to check image has not been freed
{{CodeStart}}
{{CodeStart}}
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(500, 500, 32)
{{Text|<nowiki>'The $UNSTABLE command may not be necessary if HTTP integration has been fully accepted into QB64PE.</nowiki>|#919191}}
i = {{Cl|_LOADIMAGE}}("qb64_trans.png", 32)
{{Text|<nowiki>'Feel free to remark it out if the IDE flags the following line with an ERROR message.</nowiki>|#919191}}
{{Cl|_PUTIMAGE}} (0, 0), i
{{Text|<nowiki>'And kindly report the issue on our forums or Discord so that we can update this page to keep it as 100% relevant, as possible.</nowiki>|#919191}}
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}
 
m = {{Cl|_MEMIMAGE}}(i)
{{Cm|$UNSTABLE}}:HTTP
'try uncommenting the following line and see what happens
 
'{{Cl|_MEMFREE}} m
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}({{Text|500|#F580B1}}, {{Text|500|#F580B1}}, {{Text|32|#F580B1}})
t = m.{{Cl|TYPE}}
 
{{Cl|IF...THEN|IF}} t {{Cl|AND (boolean)|AND}} 2048 {{Cl|THEN}}
Image$ = {{Text|Download$|#55FF55}}({{Text|<nowiki>"https://qb64phoenix.com/qb64wiki/resources/assets/peWikiLogo.png"</nowiki>|#FFB100}}, statusCode&) {{Text|<nowiki>'Let's try and download the QB64PE Logo from the web</nowiki>|#919191}}
  {{Cl|PRINT}} "this is/was an image"
{{Cl|IF}} statusCode& = {{Text|200|#F580B1}} {{Cl|THEN}} {{Text|<nowiki>'                                      200 says a proper connection was made to the web page in question</nowiki>|#919191}}
  {{Cl|IF...THEN|IF}} {{Cl|_MEMEXISTS}}(m) {{Cl|THEN}} 'check if memory m is still available
    i = {{Cl|_LOADIMAGE}}(Image$, {{Text|32|#F580B1}}, {{Text|<nowiki>"memory"</nowiki>|#FFB100}}) {{Text|<nowiki>'                      and then we load it for use as a registered imange</nowiki>|#919191}}
    {{Cl|PRINT}} t {{Cl|AND (boolean)|AND}} 7; "bytes per pixel"
{{Cl|ELSE}}
    {{Cl|PRINT}} "image handle "; m.IMAGE
    {{Cl|PRINT}} {{Text|<nowiki>"HTTP ERROR"</nowiki>|#FFB100}}; statusCode {{Text|<nowiki>'                            can't get a proper connection to our webpage, so we don't have an image to work with.</nowiki>|#919191}}
    {{Cl|PRINT}} "image width"; {{Cl|_WIDTH (function)|_WIDTH}}(m.IMAGE)
    {{Cl|END}} {{Text|<nowiki>'                                                        end and go report the issue on the forums, if you'd be so kind, dear user.</nowiki>|#919191}}
    {{Cl|PRINT}} "image height"; {{Cl|_HEIGHT}}(m.IMAGE)
{{Cl|END IF}}
  {{Cl|ELSE}} {{Cl|PRINT}} "Memory already freed!"
 
  {{Cl|END IF}}
{{Cl|_PUTIMAGE}} ({{Text|0|#F580B1}}, {{Text|0|#F580B1}})-({{Text|500|#F580B1}}, {{Text|500|#F580B1}}), i {{Text|<nowiki>'                                put the image on the screen so we can view it</nowiki>|#919191}}
{{Cl|END IF}}  
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}: m = {{Cl|_MEMIMAGE}}(i) {{Text|<nowiki>'                                make a memblock and point it towards our image</nowiki>|#919191}}
 
 
{{Text|<nowiki>'                                                           **** try uncommenting the following line and see what happens ****</nowiki>|#919191}}
{{Text|<nowiki>'_MEMFREE m</nowiki>|#919191}}
 
 
{{Cl|IF}} m.TYPE {{Cl|AND (boolean)|AND}} {{Text|2048|#F580B1}} {{Cl|THEN}}
    {{Cl|PRINT}} {{Text|<nowiki>"this is/was an image"</nowiki>|#FFB100}}
    {{Cl|IF}} {{Cl|_MEMEXISTS}}(m) {{Cl|THEN}} {{Text|<nowiki>'                                     check if memory m is still available</nowiki>|#919191}}
        {{Cl|PRINT}} t {{Cl|AND}} {{Text|7|#F580B1}}; {{Text|<nowiki>"bytes per pixel"</nowiki>|#FFB100}}
        {{Cl|PRINT}} {{Text|<nowiki>"image handle "</nowiki>|#FFB100}}; m.IMAGE
        {{Cl|PRINT}} {{Text|<nowiki>"image width"</nowiki>|#FFB100}}; {{Cl|_WIDTH (function)|_WIDTH}}(m.IMAGE)
        {{Cl|PRINT}} {{Text|<nowiki>"image height"</nowiki>|#FFB100}}; {{Cl|_HEIGHT}}(m.IMAGE)
    {{Cl|ELSE}} {{Text|<nowiki>'                                                      if we removed the remark from the _MEMFREE above, we'll see the following message</nowiki>|#919191}}
        {{Cl|PRINT}} {{Text|<nowiki>"Memory already freed!"</nowiki>|#FFB100}}
    {{Cl|END IF}}
{{Cl|END IF}}
 
 
{{Text|<nowiki>' Content of the HTTP response is returned.</nowiki>|#919191}}
{{Text|<nowiki>' The statusCode is also assigned.</nowiki>|#919191}}
{{Cl|FUNCTION}} {{Text|Download$|#55FF55}} (url {{Cl|AS}} {{Cl|STRING}}, statusCode {{Cl|AS}} {{Cl|LONG}})
    {{Cl|DIM}} h {{Cl|AS}} {{Cl|LONG}}, content {{Cl|AS}} {{Cl|STRING}}, s {{Cl|AS}} {{Cl|STRING}}
    h = {{Cl|_OPENCLIENT}}({{Text|<nowiki>"HTTP:"</nowiki>|#FFB100}} + url)
 
    statusCode = {{Cl|_STATUSCODE}}(h)
 
    {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(h)
        {{Cl|_LIMIT}} {{Text|60|#F580B1}}
        {{Cl|GET}} #h, , s
        content = content + s
    {{Cl|WEND}}
    {{Cl|CLOSE}} #h
 
    {{Text|Download$|#55FF55}} = content
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}




''Example 2:'' Converts the current [[_DEST|destination]] [[SCREEN]] 13 image memory altered by [[PSET]] to a [[STRING]] value. SCREEN 13 only.
''Example 2:'' Converts the current [[_DEST|destination]] [[SCREEN]] 13 image memory altered by [[PSET]] to a [[STRING]] value. SCREEN 13 only.
{{CodeStart}}
{{CodeStart}}
{{Cl|SCREEN}} 13
{{Cl|SCREEN}} {{Text|13|#F580B1}}
{{Cl|PSET}} (0, 0), {{Cl|ASC}}("H") 'top left corner of screen
{{Cl|PSET}} ({{Text|0|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|ASC (function)|ASC}}({{Text|<nowiki>"H"</nowiki>|#FFB100}}) {{Text|<nowiki>'put the ASCII value of "H" into the top left corner of screen, which is the first byte of screen image memory</nowiki>|#919191}}
{{Cl|PSET}} (1, 0), {{Cl|ASC}}("E")
{{Cl|PSET}} ({{Text|1|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|ASC (function)|ASC}}({{Text|<nowiki>"E"</nowiki>|#FFB100}}) {{Text|<nowiki>'put the ASCII value of "E" into the 2nd byte of screen image memory</nowiki>|#919191}}
{{Cl|PSET}} (2, 0), {{Cl|ASC}}("L")
{{Cl|PSET}} ({{Text|2|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|ASC (function)|ASC}}({{Text|<nowiki>"L"</nowiki>|#FFB100}}) {{Text|<nowiki>'put the ASCII value of "L" into the 3nd byte of screen image memory</nowiki>|#919191}}
{{Cl|PSET}} (3, 0), {{Cl|ASC}}("L")
{{Cl|PSET}} ({{Text|3|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|ASC (function)|ASC}}({{Text|<nowiki>"L"</nowiki>|#FFB100}}) {{Text|<nowiki>'put the ASCII value of "L" into the 4th byte of screen image memory</nowiki>|#919191}}
{{Cl|PSET}} (4, 0), {{Cl|ASC}}("O")
{{Cl|PSET}} ({{Text|4|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|ASC (function)|ASC}}({{Text|<nowiki>"O"</nowiki>|#FFB100}}) {{Text|<nowiki>'put the ASCII value of "O" into the 5th byte of screen image memory                                                                                                                                                                                                            'put the ASCII value of "E" into the 2nd byte of screen image memory</nowiki>|#919191}}
 
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}} {{Text|<nowiki>'                        define m as a mem block</nowiki>|#919191}}
m = {{Cl|_MEMIMAGE}}({{Text|0|#F580B1}}) {{Text|<nowiki>'                      point m to where our screen exists in memory</nowiki>|#919191}}


{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}
x1$ = {{Cl|_MEMGET (function)|_MEMGET}}(m, m.OFFSET, {{Cl|STRING}} * {{Text|5|#F580B1}}) {{Text|<nowiki>'m is the mem block that we're wanting to get information from</nowiki>|#919191}}
m = {{Cl|_MEMIMAGE}}(0)  'copy the screen memory to m
{{Text|<nowiki>'                                      m.OFFSET is the mem block m starting position</nowiki>|#919191}}
x1$ = {{Cl|_MEMGET (function)|_MEMGET}}(m, m.OFFSET, {{Cl|STRING}} * 5) 'get at block start position
{{Text|<nowiki>'                                      STRING * 5 is the size and type of information that we want to get from that position in memory.</nowiki>|#919191}}
{{Cl|LOCATE}} 2, 1:{{Cl|PRINT}} {{Cl|LEN}}(x1$) 'prints 5 bytes as size is STRING * 5
 
{{Cl|PRINT}} x1$ 'prints HELLO as ASCII character values
{{Cl|LOCATE}} {{Text|2|#F580B1}}, {{Text|1|#F580B1}}
{{Cl|PRINT}} m.OFFSET; m.SIZE; m.ELEMENTSIZE
{{Cl|PRINT}} {{Cl|LEN}}(x1$) {{Text|<nowiki>'                       prints 5 bytes as we deliberately fetched STRING * 5 bytes with our _MEMGET above.</nowiki>|#919191}}
{{Cl|_MEMFREE}} m  
{{Cl|PRINT}} x1$ {{Text|<nowiki>'                             prints the contents of that 5-byte string which we got above -- which is "HELLO" as CHR$() string character values</nowiki>|#919191}}
{{Cl|_MEMFREE}} m
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
  5
  5
HELLO
HELLO
5448320  6400  1
 
{{OutputEnd}}
{{OutputEnd}}
: ''Explanation:'' When a numerical [[_BYTE]] value is converted to a [[STRING]], each byte is converted to an [[ASCII]] character. The QB64 IDE will capitalize _MEM dot values.
 
{{WhiteStart}}                                        m.SIZE = 320 * 200 = 6400 bytes
                                        m.ELEMENTSIZE = 1 byte
{{WhiteEnd}}


''Example 3:'' Using _MEM to convert _OFFSET to _INTEGER64.
''Example 3:'' Using _MEM to convert _OFFSET to _INTEGER64.
{{CodeStart}}
{{CodeStart}}
DIM x AS INTEGER
{{Cl|DIM}} x {{Cl|AS}} {{Cl|INTEGER}}
DIM m AS _MEM
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}
m = _MEM(x)
m = {{Cl|_MEM (function)|_MEM}}(x)
PRINT m.OFFSET
{{Cl|PRINT}} m.OFFSET
PRINT ConvertOffset(m.OFFSET)
{{Cl|PRINT}} {{Text|ConvertOffset|#55FF55}}(m.OFFSET)




FUNCTION ConvertOffset&& (value AS _OFFSET)
{{Cl|FUNCTION}} {{Text|ConvertOffset&&|#55FF55}} (value {{Cl|AS}} {{Cl|_OFFSET (function)|_OFFSET}})
$CHECKING:OFF
    {{Cm|$CHECKING}}:{{Cl|OFF}}
DIM m AS _MEM 'Define a memblock
    {{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}} {{Text|<nowiki>'Define a memblock</nowiki>|#919191}}
m = _MEM(value) 'Point it to use value
    m = {{Cl|_MEM (function)|_MEM}}(value) {{Text|<nowiki>'Point it to use value</nowiki>|#919191}}
$IF 64BIT THEN
    {{Cm|$IF}} {{Text|64BIT|#F580B1}} {{Cm|THEN}}
    'On 64 bit OSes, an OFFSET is 8 bytes in size. We can put it directly into an Integer64
        {{Cl|DIM}} temp {{Cl|AS}} {{Cl|_INTEGER64}} {{Text|<nowiki>'On 64 bit OSes, an OFFSET is 8 bytes in size.</nowiki>|#919191}}
     _MEMGET m, m.OFFSET, ConvertOffset&& 'Get the contents of the memblock and put the values there directly into ConvertOffset&&
     {{Cm|$ELSE}}
$ELSE
        {{Cl|DIM}} temp {{Cl|AS}} {{Cl|LONG}} {{Text|<nowiki>'     However, on 32 bit OSes, an OFFSET is only 4 bytes.</nowiki>|#919191}}
    'However, on 32 bit OSes, an OFFSET is only 4 bytes. We need to put it into a LONG variable first
    {{Cm|$END IF}}
     _MEMGET m, m.OFFSET, temp& 'Like this
     {{Cl|_MEMGET}} m, m.OFFSET, temp {{Text|<nowiki>'Once we've sized our variable correctly, let's get it</nowiki>|#919191}}
     ConvertOffset&& = temp& 'And then assign that long value to ConvertOffset&&
     {{Text|ConvertOffset&&|#55FF55}} = temp {{Text|<nowiki>'   And then assign that long value to ConvertOffset&&</nowiki>|#919191}}
$END IF
    {{Cl|_MEMFREE}} m {{Text|<nowiki>'               Free the memblock</nowiki>|#919191}}
_MEMFREE m 'Free the memblock
    {{Cm|$CHECKING}}:{{Cl|ON}}
$CHECKING:ON
{{Cl|END FUNCTION}}
END FUNCTION
{{CodeEnd}}


{{CodeEnd}}


''Explanation:'' The above will print two numbers which should match.  These numbers will vary, as they're representations of where X is stored in memory, and that position is going to vary every time the program is run.  What it should illustrate, however, is a way to convert _OFFSET to _INTEGER64 values, which can sometimes be useful when trying to run calculations involving mem.SIZE, mem.TYPE, or mem.ELEMENTSIZE.
''Explanation:'' The above will print two numbers which should match.  These numbers will vary, as they're representations of where X is stored in memory, and that position is going to vary every time the program is run.  What it should illustrate, however, is a way to convert _OFFSET to _INTEGER64 values, which can sometimes be useful when trying to run calculations involving mem.SIZE, mem.TYPE, or mem.ELEMENTSIZE.





Latest revision as of 07:25, 23 September 2023

The _MEM variable type can be used when working with memory blocks. It has no variable type suffix.


Syntax

DIM m AS _MEM


Description

Variable TYPE:

  • Memory DOT values are actually part of the built in memory variable type in QB64. The following TYPE is built in:
TYPE memory_type
  OFFSET AS _OFFSET       'start location of block(changes with byte position)
  SIZE AS _OFFSET         'size of block remaining at offset(changes with position)
  TYPE AS _OFFSET         'type description of variable used(never changes)
  ELEMENTSIZE AS _OFFSET  'byte size of values inside the block(never changes)
  IMAGE AS LONG           'the image handle used when _MEMIMAGE(handle) is used
  SOUND AS LONG           'the sound handle used when _MEMSOUND(handle) is used
END TYPE

The above TYPE is for clarification purposes only. It doesn't need to be pasted in
a program to use _MEM.
IMPORTANT NOTE: As of Build 20170802/57 onward (early v1.2 development), mem.TYPE has
been changed to be an _OFFSET, just as mem.SIZE and mem.ELEMENTSIZE.

Usage

  • The _MEM type contains the following read-only elements where name is the _MEM variable name:
name.OFFSET is the current start position in the memory block AS _OFFSET. Add bytes to change position.
name.SIZE is the remaining size of the block at current position in bytes AS _OFFSET
name.TYPE is the type (represented as bits combined to form a value) AS _OFFSET:


.TYPE values (version 1.000 and up incl. all QB64-PE releases)

  • [bit 0] 1* byte types (_BYTE)
  • [bit 1] 2* byte types (INTEGER)
  • [bit 2] 4* byte types (LONG or SINGLE)
  • [bit 3] 8* byte types (DOUBLE or _INTEGER64)
  • [bit 4] 16* byte types (reserved for future use)
  • [bit 5] 32* byte types (_FLOAT)
  • [bit 6] 64* byte types (reserved for future use)
  • [bit 7] 128 = integer types (_BYTE, INTEGER, LONG, _INTEGER64) (added to *)
  • [bit 8] 256 = floating point types (SINGLE, DOUBLE, _FLOAT) (added to *)
  • [bit 9] 512 = STRING types (fixed length or variable length)
  • [bit 10] 1024 = _UNSIGNED types (added to *+128)
  • [bit 11] 2048 = pixel data usually from _MEMIMAGE (added to 1+128+1024 for 256 color screens, or 2+128+1024 for text screens, or 4+128+1024 for 32-bit color screens)
  • [bit 12] 4096 = _MEM TYPE structure (NOT added to 32768)
  • [bit 13] 8192 = _OFFSET type (added to 4+128+[1024] or 8+128+[1024] or future_size+128+[1024])
  • [bit 14] 16384 = data created/defined by _MEMNEW(size) or _MEMNEW(offset,size)
  • [bit 15] 32768 = a custom, user defined type (ie. created with TYPE name ... END TYPE)
  • [bit 16] 65536 = an array of data (added to other type values defining the array's data type)

Note: If a future integer, float or other type doesn't have a size that is 1,2,4,8,16,32,64,128 or 256 it won't have a size-bit set.

Versions prior to 1.000 (never use with QB64-PE releases)

  • Note: _OFFSET values cannot be cast to other variable types reliably. _MEM is a reserved custom variable type.
  • _MEM cannot reference variable length STRING variable values. String values must be designated as a fixed-length string.


Examples

Example 1: Demonstration of .IMAGE to determine an image's dimensions, .TYPE to verify the type and _MEMEXISTS to check image has not been freed

'The $UNSTABLE command may not be necessary if HTTP integration has been fully accepted into QB64PE.
'Feel free to remark it out if the IDE flags the following line with an ERROR message.
'And kindly report the issue on our forums or Discord so that we can update this page to keep it as 100% relevant, as possible.

$UNSTABLE:HTTP

SCREEN _NEWIMAGE(500, 500, 32)

Image$ = Download$("https://qb64phoenix.com/qb64wiki/resources/assets/peWikiLogo.png", statusCode&) 'Let's try and download the QB64PE Logo from the web
IF statusCode& = 200 THEN '                                      200 says a proper connection was made to the web page in question
    i = _LOADIMAGE(Image$, 32, "memory") '                       and then we load it for use as a registered imange
ELSE
    PRINT "HTTP ERROR"; statusCode '                             can't get a proper connection to our webpage, so we don't have an image to work with.
    END '                                                        end and go report the issue on the forums, if you'd be so kind, dear user.
END IF

_PUTIMAGE (0, 0)-(500, 500), i '                                 put the image on the screen so we can view it
DIM m AS _MEM: m = _MEMIMAGE(i) '                                make a memblock and point it towards our image


'                                                           **** try uncommenting the following line and see what happens ****
'_MEMFREE m


IF m.TYPE AND 2048 THEN
    PRINT "this is/was an image"
    IF _MEMEXISTS(m) THEN '                                      check if memory m is still available
        PRINT t AND 7; "bytes per pixel"
        PRINT "image handle "; m.IMAGE
        PRINT "image width"; _WIDTH(m.IMAGE)
        PRINT "image height"; _HEIGHT(m.IMAGE)
    ELSE '                                                       if we removed the remark from the _MEMFREE above, we'll see the following message
        PRINT "Memory already freed!"
    END IF
END IF


' Content of the HTTP response is returned.
' The statusCode is also assigned.
FUNCTION Download$ (url AS STRING, statusCode AS LONG)
    DIM h AS LONG, content AS STRING, s AS STRING
    h = _OPENCLIENT("HTTP:" + url)

    statusCode = _STATUSCODE(h)

    WHILE NOT EOF(h)
        _LIMIT 60
        GET #h, , s
        content = content + s
    WEND
    CLOSE #h

    Download$ = content
END FUNCTION


Example 2: Converts the current destination SCREEN 13 image memory altered by PSET to a STRING value. SCREEN 13 only.

SCREEN 13
PSET (0, 0), ASC("H") 'put the ASCII value of "H" into the top left corner of screen, which is the first byte of screen image memory
PSET (1, 0), ASC("E") 'put the ASCII value of "E" into the 2nd byte of screen image memory
PSET (2, 0), ASC("L") 'put the ASCII value of "L" into the 3nd byte of screen image memory
PSET (3, 0), ASC("L") 'put the ASCII value of "L" into the 4th byte of screen image memory
PSET (4, 0), ASC("O") 'put the ASCII value of "O" into the 5th byte of screen image memory                                                                                                                                                                                                            'put the ASCII value of "E" into the 2nd byte of screen image memory

DIM m AS _MEM '                         define m as a mem block
m = _MEMIMAGE(0) '                      point m to where our screen exists in memory

x1$ = _MEMGET(m, m.OFFSET, STRING * 5) 'm is the mem block that we're wanting to get information from
'                                       m.OFFSET is the mem block m starting position
'                                       STRING * 5 is the size and type of information that we want to get from that position in memory.

LOCATE 2, 1
PRINT LEN(x1$) '                        prints 5 bytes as we deliberately fetched STRING * 5 bytes with our _MEMGET above.
PRINT x1$ '                             prints the contents of that 5-byte string which we got above -- which is "HELLO" as CHR$() string character values
_MEMFREE m
 5
HELLO


Example 3: Using _MEM to convert _OFFSET to _INTEGER64.

DIM x AS INTEGER
DIM m AS _MEM
m = _MEM(x)
PRINT m.OFFSET
PRINT ConvertOffset(m.OFFSET)


FUNCTION ConvertOffset&& (value AS _OFFSET)
    $CHECKING:OFF
    DIM m AS _MEM 'Define a memblock
    m = _MEM(value) 'Point it to use value
    $IF 64BIT THEN
        DIM temp AS _INTEGER64 'On 64 bit OSes, an OFFSET is 8 bytes in size.
    $ELSE
        DIM temp AS LONG '      However, on 32 bit OSes, an OFFSET is only 4 bytes.
    $END IF 
    _MEMGET m, m.OFFSET, temp 'Once we've sized our variable correctly, let's get it
    ConvertOffset&& = temp '   And then assign that long value to ConvertOffset&&
    _MEMFREE m '               Free the memblock
    $CHECKING:ON
END FUNCTION


Explanation: The above will print two numbers which should match. These numbers will vary, as they're representations of where X is stored in memory, and that position is going to vary every time the program is run. What it should illustrate, however, is a way to convert _OFFSET to _INTEGER64 values, which can sometimes be useful when trying to run calculations involving mem.SIZE, mem.TYPE, or mem.ELEMENTSIZE.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link