Test yourself
Code: Select all
var$=PeekS(Ascii(#CRLF$),2,#PB_Ascii); <- Not Ok ??
debug peekB(@var$)
debug peekB(@var$+1)
debug "-----------"
CopyMemory(Ascii(#CRLF$),@var$,2); <- Ok
debug peekB(@var$)
debug peekB(@var$+1)

Code: Select all
var$=PeekS(Ascii(#CRLF$),2,#PB_Ascii); <- Not Ok ??
debug peekB(@var$)
debug peekB(@var$+1)
debug "-----------"
CopyMemory(Ascii(#CRLF$),@var$,2); <- Ok
debug peekB(@var$)
debug peekB(@var$+1)

Code: Select all
var$=PeekS(Ascii(#CRLF$),2,#PB_Ascii)Code: Select all
var$=PeekS(Ascii(#CRLF$),2,#PB_Ascii)
Debug PeekC(@var$)
Debug PeekC(@var$+ SizeOf(Character))

Code: Select all
var$=PeekS(Ascii(#CRLF$),2,#PB_Ascii)Code: Select all
var$=PeekS(Ascii(#CRLF$), 1) ; -1
Debug PeekB(@var$)
Debug PeekB(@var$+1)Reads ascii, converts to unicode
Code: Select all
Ascii(#CRLF$)Code: Select all
),2,#PB_AsciiCode: Select all
var$=PeekS(Ascii(#CRLF$),1) ; -1
Debug Asc(var$)
Debug Len(var$)
ShowMemoryViewer(@var$, 2)
Debug $0D
Debug $0A*Buffer = Ascii(String$)
......
The buffer includes a null-terminated character.
Code: Select all
;;var$=PeekS(Ascii(#CRLF$), 2) ; Read 2 characters (=4 bytes) in Unicode format from that memory.
var$=PeekS(Ascii(#CRLF$), 1) ; Read 1 character (=2 bytes) in Unicode format from that memory.
; var$=PeekS(Ascii(#CRLF$), 1, #PB_Unicode) ; same
Debug PeekB(@var$)
Debug PeekB(@var$+1)
Code: Select all
EnableExplicit
Global str.s
str = PeekS(?hello,-1,#PB_Ascii)
Debug str
DataSection
hello:
!db 'Hello World',0
EndDataSection

CopyMemory(Ascii(#CRLF$),@var$,Len(#CRLF$)+1); <- OkSTARGÅTE wrote: Sat Sep 21, 2024 9:22 pm Using CopyMemory is incorrect. It leads to IMAs, is the string was not allocated before in a sufficient length.
Code: Select all
EnableExplicit
Global *test.String
Global *memory
Global length.i
Procedure.i AsciiAlloc(String.s);<- some black magic
Protected *str.Integer
Protected *eod.Ascii
*str = Ascii(Space(SizeOf(Integer)) + String + ".")
If *str
*str\i = *str + SizeOf(Integer)
*eod = *str + MemorySize(*str) - 2
*eod\a = 0
ProcedureReturn *str
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i AsciiDebug(*String.Integer);<- only for debugging
ShowMemoryViewer(*String\i,MemorySize(*String) - SizeOf(Integer) - 1)
ProcedureReturn #Null
EndProcedure
Procedure.i AsciiMemory(*String.Integer,*Length.Integer = #Null);<- get information about the memory
If *Length
*Length\i = MemorySize(*String) - SizeOf(Integer) - 2
EndIf
ProcedureReturn *String\i
EndProcedure
Procedure.i AsciiFree(*String)
FreeMemory(*String)
ProcedureReturn #Null
EndProcedure
*test = AsciiAlloc("Hello World")
*memory = AsciiMemory(*test,@length)
Debug *test\s ;<- assigned to string...
Debug *memory ;<- ascii string in memory
Debug length ;<- ascii string length
AsciiDebug(*test) ;<- show ascii string in memory
AsciiFree(*test) ;<- free ascii string
End

Code: Select all
Procedure$ AsignarCadenaAscii(cadena$)
;Carga una cadena de caracteres ascii extendido en una variable
Protected size.i=2*Round((Len(cadena$)+1)/2,#PB_Round_Up)
Protected *mem.byte=AllocateMemory(size,#PB_Memory_NoClear)
*mem=Ascii(cadena$)
ProcedureReturn PeekS(*mem,size)
EndProcedure
c$="Hello World !"
var$=AsignarCadenaAscii(c$)
debug var$
ShowMemoryViewer(@var$,2*Round((Len(c$)+1)/2,#PB_Round_Up))
The following code has a memory leak:Psychophanta wrote: Mon Sep 23, 2024 7:19 pm @Mijikai
That's correct. Just it has to be stored in a string variable instead of pointer, like this one, which i think this is totally safe:Code: Select all
Procedure$ AsignarCadenaAscii(cadena$) ;Carga una cadena de caracteres ascii extendido en una variable Protected size.i=2*Round((Len(cadena$)+1)/2,#PB_Round_Up) Protected *mem.byte=AllocateMemory(size,#PB_Memory_NoClear) *mem=Ascii(cadena$) ProcedureReturn PeekS(*mem,size) EndProcedure c$="Hello World !" var$=AsignarCadenaAscii(c$) debug var$ ShowMemoryViewer(@var$,2*Round((Len(c$)+1)/2,#PB_Round_Up))
Code: Select all
Procedure$ AsignarCadenaAscii(cadena$)
;Carga una cadena de caracteres ascii extendido en una variable
Protected size.i=2*Round((Len(cadena$)+1)/2,#PB_Round_Up)
Protected *mem.byte=AllocateMemory(size,#PB_Memory_NoClear) ; <<<< this memory is never freed after the address is list on the next statement
*mem=Ascii(cadena$) ;<<<< this memory is never freed, only copied
ProcedureReturn PeekS(*mem,size)
EndProcedure