Difference between revisions of "MEMNEW"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 14: Line 14:
  
  
''Example:  
+
''Example:'' Shows how [[SINGLE]] numerical values can be passed, but unfixed [[STRING]] lengths cannot get the value.
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
 
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}
 
{{Cl|DIM}} m {{Cl|AS}} {{Cl|_MEM}}
 
{{Cl|DIM}} f {{Cl|AS}} {{Cl|STRING}} * 5
 
{{Cl|DIM}} f {{Cl|AS}} {{Cl|STRING}} * 5
 
m = {{Cl|_MEMNEW}}(5) 'create new memory block of 5 bytes
 
m = {{Cl|_MEMNEW}}(5) 'create new memory block of 5 bytes
a = 12345
+
a = 12345.6
{{Cl|_MEMPUT}} m, m.OFFSET, a
+
{{Cl|_MEMPUT}} m, m.OFFSET, a 'put single value
{{Cl|_MEMGET}} m, m.OFFSET, b
+
{{Cl|_MEMGET}} m, m.OFFSET, b 'get single value
{{Cl|PRINT}} b
+
{{Cl|PRINT}} "b = "; b
 
c$ = "Doggy"
 
c$ = "Doggy"
{{Cl|_MEMPUT}} m, m.OFFSET, c$
+
{{Cl|_MEMPUT}} m, m.OFFSET, c$ 'put 5 byte string value
{{Cl|_MEMGET}} m, m.OFFSET, d$
+
{{Cl|_MEMGET}} m, m.OFFSET, d$ 'get unfixed length string value
{{Cl|_MEMGET}} m, m.OFFSET, f
+
{{Cl|_MEMGET}} m, m.OFFSET, f 'get 5 byte string value
e$ = {{Cl|_MEMGET}}(m, m.OFFSET, {{Cl|STRING}} * 5)
+
e$ = {{Cl|_MEMGET (function)|_MEMGET}}(m, m.OFFSET, {{Cl|STRING}} * 5) 'get 5 byte string value
{{Cl|PRINT}} "d$ = ";d$
+
{{Cl|PRINT}} "d$ = "; d$; {{Cl|LEN}}(d$) 'prints empty string
{{Cl|PRINT}} "e$ = ";e$
+
{{Cl|PRINT}} "e$ = "; e$; {{Cl|LEN}}(e$)
{{Cl|PRINT}} "f = "; '' ''
+
{{Cl|PRINT}} "f = "; f; {{Cl|LEN}}(f) '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
 +
{{OutputStart}}b =  12345.6
 +
d$ =  0
 +
e$ = doggy 5
 +
f = doggy 5 {{OutputEnd}}
 +
  
  

Revision as of 06:27, 19 August 2012

The _MEMNEW function allocates new memory and returns a _MEM memory block referring to it.


Syntax

memory_block = _MEMNEW(byte_size%%)


  • The memory block value created holds the starting OFFSET, SIZE, TYPE and ELEMENTSIZE.
  • The byte size parameter is the desired byte size of the memory block.
  • All values created by this function MUST be freed using _MEMFREE!
  • When a new memory block is created the memory .TYPE value will be 0.(this may be changeable in the future)
  • If the read only memory block .SIZE is 0, the memory block was not created!


Example: Shows how SINGLE numerical values can be passed, but unfixed STRING lengths cannot get the value.

DIM m AS _MEM DIM f AS STRING * 5 m = _MEMNEW(5) 'create new memory block of 5 bytes a = 12345.6 _MEMPUT m, m.OFFSET, a 'put single value _MEMGET m, m.OFFSET, b 'get single value PRINT "b = "; b c$ = "Doggy" _MEMPUT m, m.OFFSET, c$ 'put 5 byte string value _MEMGET m, m.OFFSET, d$ 'get unfixed length string value _MEMGET m, m.OFFSET, f 'get 5 byte string value e$ = _MEMGET(m, m.OFFSET, STRING * 5) 'get 5 byte string value PRINT "d$ = "; d$; LEN(d$) 'prints empty string PRINT "e$ = "; e$; LEN(e$) PRINT "f = "; f; LEN(f)

b = 12345.6 d$ = 0 e$ = doggy 5 f = doggy 5


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page