MEMNEW: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_MEMNEW}} The _MEMNEW function allocates new memory and returns a _MEM memory block referring to it. {{PageSyntax}} : {{Parameter|memoryBlock}} = _MEMNEW({{Parameter|byteSize}}) {{Parameters}} * The {{Parameter|byteSize}} parameter is the desired byte size of the memory block based on the variable type it will hold. {{PageDescription}} * The {{Parameter|memoryBlock}} value created holds the elements .OFFSET, .SIZE, .TYPE and .ELEMENTS...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 7: Line 7:




{{Parameters}}
{{PageParameters}}
* The {{Parameter|byteSize}} parameter is the desired byte size of the memory block based on the variable [[type]] it will hold.
* The {{Parameter|byteSize}} parameter is the desired byte size of the memory block based on the variable [[Variable Types|type]] it will hold.




{{PageDescription}}
{{PageDescription}}
* The {{Parameter|memoryBlock}} value created holds the elements .OFFSET, .SIZE, .TYPE and .ELEMENTSIZE.  
* The {{Parameter|memoryBlock}} value created holds the elements .OFFSET, .SIZE, .TYPE and .ELEMENTSIZE.
* [[_MEMNEW]] does not clear the data previously in the memory block it allocates, for speed purposes.
* [[_MEMNEW]] does not clear the data previously in the memory block it allocates, for speed purposes.
* To clear previous data from a new memory block, use [[_MEMFILL]] with a byte value of 0.
* To clear previous data from a new memory block, use [[_MEMFILL]] with a byte value of 0.
Line 22: Line 22:
{{PageExamples}}
{{PageExamples}}
''Example:'' Shows how [[SINGLE]] numerical values can be passed, but non-fixed [[STRING]] lengths cannot get the value.
''Example:'' Shows how [[SINGLE]] numerical values can be passed, but non-fixed [[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
Line 37: Line 37:
{{Cl|PRINT}} "d$ = "; d$; {{Cl|LEN}}(d$) 'prints empty string
{{Cl|PRINT}} "d$ = "; d$; {{Cl|LEN}}(d$) 'prints empty string
{{Cl|PRINT}} "e$ = "; e$; {{Cl|LEN}}(e$)
{{Cl|PRINT}} "e$ = "; e$; {{Cl|LEN}}(e$)
{{Cl|PRINT}} "f = "; f; {{Cl|LEN}}(f) '' ''
{{Cl|PRINT}} "f = "; f; {{Cl|LEN}}(f)
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}b =  12345.6
{{OutputStart}}b =  12345.6
Line 43: Line 43:
e$ = Doggy 5
e$ = Doggy 5
f = Doggy 5 {{OutputEnd}}
f = Doggy 5 {{OutputEnd}}





Latest revision as of 20:27, 26 January 2024

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


Syntax

memoryBlock = _MEMNEW(byteSize)


Parameters

  • The byteSize parameter is the desired byte size of the memory block based on the variable type it will hold.


Description

  • The memoryBlock value created holds the elements .OFFSET, .SIZE, .TYPE and .ELEMENTSIZE.
  • _MEMNEW does not clear the data previously in the memory block it allocates, for speed purposes.
  • To clear previous data from a new memory block, use _MEMFILL with a byte value of 0.
  • When a new memory block is created the memory .TYPE value will be 0.
  • If the read only memory block .SIZE is 0, the memory block was not created.
  • All values created by memory functions must be freed using _MEMFREE with a valid _MEM variable.


Examples

Example: Shows how SINGLE numerical values can be passed, but non-fixed 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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link