Poke Modifying Previous Address' Value

General FreeBASIC programming questions.
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Poke Modifying Previous Address' Value

Post by Username »

(I'm using a 32 bit computer and 32 bit FB. I also have indeed used allocate beforehand)
When I use poke on an address, the previous address is modified.

Code: Select all

Poke UInteger, pointer + 320,10
Print Peek(UInteger, pointer + 319)
pointer + 319 is 0 before the poke.
Thanks
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Poke Modifying Previous Address' Value

Post by MrSwiss »

Sorry, the posted code-fragment makes "no sense at all" as it is.

Why don't elaborete some more, about "what you want to achieve" with the code?
Alternatively, post the whole code, that deals with the issue ...
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

ok i will post more soon
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Post by fxm »

Where is the problem?

Code: Select all

Dim As Uinteger Ptr p = Allocate(350 * Sizeof(UInteger))

Print Peek(UInteger, p + 319)
Print Peek(UInteger, p + 320)
Print Peek(UInteger, p + 321)
Print
Poke UInteger, p + 320, 10
Print Peek(UInteger, p + 319)
Print Peek(UInteger, p + 320)
Print Peek(UInteger, p + 321)

Deallocate(p)
Sleep
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

MrSwiss wrote:Sorry, the posted code-fragment makes "no sense at all" as it is.

Why don't elaborete some more, about "what you want to achieve" with the code?
Alternatively, post the whole code, that deals with the issue ...

Code: Select all

#LANG "fblite"
option gosub
#include "file.bi"
dim Shared a as string
dim Shared quitonerror as ubyte
dim Shared ifusechecksum as ubyte
dim Shared eror as UByte
Dim Shared eror2 As UByte
option gosub
dim Shared c as UInteger
Dim Shared shoit As UInteger
Dim Shared shooit As UInteger
Dim Shared shoooit As Any Ptr
dim Shared d as UInteger
dim Shared fard As UInteger
Dim Shared farrd As Uinteger
dim Shared memm as any Ptr
Dim Shared de As Any Ptr
Dim Shared shortstr As String * 4
Dim Shared progscreen(32) As Any Ptr
Dim Shared bufffer(10) As UInteger

Public Function errorfn (ByVal message As String)
	?"*********************************"
	?message
	?"*********************************"
	Input "",a
End Function
Public Function SPSMtC (ByVal red As UInteger, ByVal green As UInteger, ByVal blue As UInteger)
	If red>255 Then errorfn("ERROR (4). RGB VALUE OUT OF BOUNDS."):shooit = 1
	If green>255 Then errorfn("ERROR (4). RGB VALUE OUT OF BOUNDS."):shooit = 1
	If blue>255 Then errorfn("ERROR (4). RGB VALUE OUT OF BOUNDS."):shooit = 1
	For shoit = peek(uinteger,peek(uinteger,memm+2)*8+64+5) To peek(uinteger,peek(uinteger,memm+2)*8+64+5)+peek(uinteger,peek(uinteger,memm+2)*8+64+6)
		If shooit <>1 Then Poke UInteger, shoit, RGB(red,green,blue)
	Next
	shooit = 0
End Function


Public Function CVMtSM (ByVal copyfrom As UInteger, ByVal copyto As UInteger)
	If copyto> Peek(UInteger,memm+7) Then errorfn("ERROR (1). ATTEMPTED MEMORY WRITE OUT OF BOUNDS."):shooit = 1
	If copyfrom>Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (2). ATTEMPTED MEMORY WRITE OUT OF VIRTUAL MEMORY BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger, memm+copyto,(Peek(UInteger,Peek(UInteger,memm+2)*8+64)+copyfrom)
	shooit = 0
End Function

Public Function CSMtVM (ByVal copyfrom As UInteger, ByVal copyto As UInteger)
	If copyfrom> Peek(UInteger,memm+7) Then errorfn("ERROR (1). ATTEMPTED MEMORY WRITE OUT OF BOUNDS."):shooit = 1
	If copyto>Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (2). ATTEMPTED MEMORY WRITE OUT OF VIRTUAL MEMORY BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger, (Peek(UInteger,Peek(UInteger,memm+2)*8+64)+copyto),memm+copyfrom
	shooit = 0
End Function

Public Function CVMtVM (ByVal copyfrom As UInteger, ByVal copyto As UInteger)
	If copyfrom> Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (3). ATTEMPTED MEMORY READ OUT OF VIRTUAL MEMORY BOUNDS."):shooit = 1
	If copyto>Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (2). ATTEMPTED MEMORY WRITE OUT OF VIRTUAL MEMORY BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger, (Peek(UInteger,Peek(UInteger,memm+2)*8+64)+copyto),peek(uinteger,peek(uinteger,memm+2)*8+64)+copyfrom
	shooit = 0
End Function

Public Function WtVM (ByVal value As UInteger, ByVal address As UInteger)
	If address>Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (2). ATTEMPTED MEMORY WRITE OUT OF VIRTUAL MEMORY BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger,peek(uinteger,peek(uinteger,memm+2)*8+64)+address,value
	shooit = 0
End Function

Public Function WtSM (ByVal value As UInteger, ByVal address As UInteger)
	If address>Peek(UInteger,memm+7) Then errorfn("ERROR (1). ATTEMPTED MEMORY WRITE OUT OF BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger,(Peek(UInteger,Peek(UInteger,memm+2)*8+64)+address),value
	shooit = 0
End Function


Public Function JtVA (ByVal location As UInteger)
	If location>Peek(UInteger,memm+2)*8+64+1 Then errorfn("ERROR (0). PROGRAM COUNTER OUT OF BOUNDS."):shooit = 1
	If shooit <> 1 Then Poke UInteger,Peek(UInteger,memm+2)*8+64+2,location
	shooit = 0
End Function

Open "C:\CCMS Environment\User improvements\Data\ProgData\CCMS\ExtraProg\Progman\ifrequire123" for input as #200
get #200,, ifusechecksum
close #200
open "C:\CCMS Environment\User improvements\Data\ProgData\CCMS\ExtraProg\Progman\ifquitoneapierror" for input as #139
get #139,, quitonerror
close #139
dim Shared dee as string *8
dim Shared scrbuf1 as ubyte, scrbuf2 as ubyte, scr3buf as ubyte, scrbuf4 as ubyte, scrbuf5 as ubyte, scrbuf6 as UByte

screen 21,24,,1
SetMouse 0,0,0
dim Shared w as integer,h as integer
dim shared scrh as uinteger, scrw as uinteger
screeninfo scrw,scrh
open "C:\CCMS Environment\User improvements\Data\ProgData\CCMS\ExtraProg\GeneralProg\CurrentUser" for binary as #99
get #99,,dee
close #99
open "C:\CCMS Environment\User improvements\Data\Users\" + dee for binary as #99
seek 99,17
get #99,,scrbuf1
get #99,,scrbuf2
get #99,,scrbuf3
get #99,,scrbuf4
get #99,,scrbuf5
get #99,,scrbuf6
color rgb(scrbuf1,scrbuf2,scrbuf3), rgb(scrbuf4,scrbuf5,scrbuf6)
cls
seek 99,26
get #99,,dee
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"

open "C:\CCMS Environment\User improvements\Data\ProgData\CCMS\ExtraProg\GeneralProg\Screenh" for input as #128
get #128,, scrh
close #128
open "C:\CCMS Environment\User improvements\Data\ProgData\CCMS\ExtraProg\GeneralProg\Screenw" for input as #128
get #128,, scrw
close #128
memm = allocate(1024 * 512)
if memm = 0 then goto 10000
Poke UInteger,memm+320,10
?Peek(UInteger,memm+319):Sleep:System
Poke UInteger, memm+7,1024*512
poke uinteger, memm + 20, 1
goto 5
1 ?"*********************************"
?"AN UNEXPECTED ERROR OCURRED."
?"*********************************"
2 ?"Press 1 to view error information."
?"Press 2 to resume execution."
?"Type anything else to quit execution."
input ">", eror
if eror = 1 then goto 3
if eror = 2 then goto 4
system
3 ?"Source code line #" & erl
?"FreeBASIC error #" & err
?"Press Enter to continue."
input "", eror
goto 2
4 resume next

5 Locate 0,0,0
Rem windowtitle "CCMS Executable Selector"
Rem if command(0) <> "" then goto 7
goto 10
Rem 7 if fileexist(command(0)) then goto 8
?"*********************************"
?"ERROR. INVALID ARGUMENTS."
?"*********************************"
input "", a
goto 10
8 poke UInteger, memm + 21, 1
goto 3200

10 Cls
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"
11 ?"Please type the program file to execute, or 'exit' to exit."
input ">", a
If a = "exit" Then system
if fileexists("C:\CCMS Environment\Programs\" + a + "\" + a) then goto 20010
?"*********************************"
?"ERROR. INVALID FILENAME."
?"*********************************"
goto 11

15 rem "Main" Loop
If Peek(UInteger,memm)=0then GoTo 10
if multikey(SC_CONTROL) And multikey(SC_LSHIFT) And multikey(SC_MENU) Then goto 10100
rem if peek(uinteger, ((64 + (8 * peek(uinteger, memm + 2)) + 3) shl 2) and 1) = 1 then goto 11100
rem if peek(uinteger, memm) > 1 ANDALSO peek(uinteger, memm + 2) = peek(uinteger, memm) then
rem poke uinteger, memm + 2, 0
rem ElseIf peek(uinteger, memm) > 1 ANDALSO peek(uinteger, memm + 2) < peek(uinteger, memm) then
rem poke uinteger, memm + 2, peek(uinteger, memm + 2) + 1
rem EndIf
For shoit = 0 To 11
	bufffer(shoit) = peek(uinteger,peek(uinteger,peek(uinteger,memm+2)*8+64+memm) + peek(uinteger,peek(uinteger,memm+2)*8+64+2+memm) + shoit)
	Rem ?:?bufffer(shoit):?peek(uinteger,peek(uinteger,memm+2)*8+64+memm) + peek(uinteger,peek(uinteger,memm+2)*8+64+2+memm) + shoit:Sleep
	?Peek(UInteger,memm+320+shoit):sleep
Next
If bufffer(0) = 0 Then WtVM(bufffer(1),bufffer(2))
If bufffer(0) = 1 Then WtSM(bufffer(1),bufffer(2))
If bufffer(0) = 2 Then CVMtVM(bufffer(1),bufffer(2))
If bufffer(0) = 3 Then CSMtVM(bufffer(1),bufffer(2))
If bufffer(0) = 4 Then CVMtSM(bufffer(1),bufffer(2))
If bufffer(0) = 5 Then SPSMtC(bufffer(1),bufffer(2),bufffer(3))
If bufffer(0) = 9 Then JtVA(bufffer(1))

17 rem ahahah
poke uinteger, memm + 31, asc(inkey)
poke uinteger, memm + 16, 0
poke uinteger, memm + 10, 0
Poke UInteger, memm+Peek(UInteger,memm+2)*8+64+2,memm+Peek(UInteger,memm+2)*8+64+2+1
If (Peek(UInteger,memm+2)*8+64)+2 > (Peek(UInteger,memm+2)*8+64)+1 Then errorfn("ERROR (0). PC OUT OF BOUNDS."):If quitonerror <> 0 Then GoTo 3050
Poke UInteger,memm+2,Peek(UInteger,memm+2)+1 Mod Peek(UInteger,memm)
GoTo 15

rem 3000 rem Iallocate
rem if peek(uinteger, memm + 6) and 1 = 1 then goto 3050
rem 3010 if peek(uinteger, memm + 6) and 2 = 2 then goto 3100
rem 3020 if peek(uinteger, memm + 6) and 4 = 4 then goto 3150
rem 3020 if peek(uinteger, memm + 6) and 8 = 8 then goto 3200

3050 rem close a program
rem clear memm + 64 + (8 * (peek(UInteger, memm) + 1)), 0, 8
if peek(uinteger, memm + 2) = peek(UInteger, memm) then
clear memm + 64 + (8 * (peek(uinteger, memm) + 1)), 0, 8
poke uinteger, memm,peek(uinteger, memm) - 1
if peek(uinteger, memm + 2) > 0 then
	poke uinteger, memm + 2,peek(uinteger, memm + 2) - 1
ElseIf peek(uinteger, memm + 2) = 0 then
	poke uinteger, memm + 2,peek(uinteger, memm + 2) + 1
End If
goto 15
End If
fb_memmove(peek(UInteger,memm + 22), (memm + 64 + (8 * peek(uinteger, memm + 2))), 8)
fb_memmove((memm + 64 + 8+ (8 * peek(uinteger, memm + 2))), (memm + 64 + (8 * peek(uinteger, memm + 2)) + 8), (8 * (peek(uinteger, memm) - peek(uinteger, memm + 2))))
Rem fb_memmove(Peek(uinteger, memm + 22), peek(uinteger, memm + 64 + (8 * peek(uinteger, memm + 2))), peek(uinteger, memm + 64 + (8 * peek(uinteger, memm)) - peek(uinteger, memm + 64 + (8 * peek(uinteger, memm + 2)))))
fb_memmove(Peek(UInteger,memm+22),peek(uinteger, memm + 64 + 8 + (8 * peek(uinteger, memm + 2))),Peek(UInteger,memm+7)-(peek(uinteger, memm + 64 + 8 + (8 * peek(uinteger, memm + 2)))-Cast(UInteger,memm)))
shoit = (8 * peek(uinteger, memm + 2))
shooit = (8 * peek(uinteger, memm))
for d = shoit To shooit step 8
poke uinteger, memm + 64 + d, Peek(uinteger, memm + 64 + d) - peek(uinteger, memm + 23)
Next d
shoit = (peek( uinteger, memm) - peek(uinteger, memm + 2))
Rem for c = 0 To shoit step 8
Rem poke UInteger, (64 + (8 * peek( uinteger, memm) - Peek(uinteger, memm + 2)))+memm, peek(uinteger, (64 + (8 * peek( uinteger, memm) - peek(uinteger, memm + 2)))) - peek(uinteger, memm + 23)
Rem Next c

poke uinteger, memm,peek(uinteger, memm) - 1
if peek(uinteger, memm + 2) > 0 then
	poke uinteger, memm + 2,peek(uinteger, memm + 2) - 1
ElseIf peek(uinteger, memm + 2) = 0 then
	poke uinteger, memm + 2,peek(uinteger, memm + 2) + 1
End if
goto 15

3200
?"hello:system

10000 ?"*********************************"
?"ERROR. NOT ENOUGH AVAILABLE MEMORY."
?"*********************************"
?"Press 1 and ENTER to try again with less memory."
?"Press ENTER to exit."
input ">", eror
if eror = 1 then goto 10010
system
10010 d = d + 1
memm = allocate((1024 * 512) - (d * 256))
if memm = 0 then goto 10000
goto 5

10100 Cls
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"
?"Options:"
?""
?"Press 1 and ENTER to close the currently active program."
?"Press 2 and ENTER to request more memory."
?"Press 3 and ENTER to launch a program."
?"Press 4 and ENTER to exit this menu."
input ">", eror
if eror = 1 then goto 3050
if eror = 2 then goto 11000
if eror = 3 then goto 20000
if eror = 4 then goto 15
?"*********************************"
?"ERROR. INVALID COMMAND."
?"*********************************"
?"Press Enter to continue."
input ">", c
goto 10100

11000 Cls
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"
?"Please enter the amount of memory (in addresses) to allocate. Enter 0 to return."
?"It may cause crashes if the value is less than 524288."
input ">", farrd
if farrd = 0 then goto 10100
de = reallocate(memm, farrd)
if de = 0 then goto 10000
memm = de
Poke UInteger,memm+7,farrd
11100 poke uinteger, (64 + (8 * peek(uinteger, memm + 2)) + 3), (64 + (8 * peek(uinteger, memm + 2)) + 3) and not (0 + 1 shr 2)
goto 17

20000 rem launch a program
Cls
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"
?"Please type the program name to run."
?"Type 'Return' to return from this menu."
input "", a
if a = "return" then goto 10100
if fileexists("C:\CCMS Environment\Programs\" + a + "\" + a) then goto 20010
?"*********************************"
?"ERROR. INVALID PROGRAM NAME."
?"*********************************"
input "", a
goto 20000
20010 rem opening the program
Cls
bload "C:\CCMS Environment\User improvements\Data\Backgrounds\" + dee + ".bmp"
open ("C:\CCMS Environment\Programs\" + a + "\" + a) for binary as #108
if ifusechecksum = 0 then goto 20050
Seek 108,3
get #108,, eror
get #108,, eror2
If eror = 123 Then If eror2 = 123 Then GoTo 20050
?"*********************************"
?"ERROR. INVALID PROGRAM HEADER."
?"*********************************"
input "", a
goto 10
20050 Rem copying program and name to RAM
If Peek(UInteger,memm) > 0 Then Poke UInteger,memm+64+8+(Peek(UInteger,memm)*8),memm+Peek(UInteger,memm+64+8+(Peek(UInteger,memm)*8)+1)
If Peek(UInteger,memm) = 0 Then Poke UInteger,memm+64+8+(Peek(UInteger,memm)*8),memm+320
Poke UInteger,memm,Peek(UInteger,memm)+1
Seek 108,11
For c = 0 To Lof(108)-11
	Get #108,,d
	?d;" ";
	Poke UInteger,peek(uinteger,(peek(UInteger,memm)*8+64)+memm)+c,d
	?"mem:"&Peek(UInteger,memm+320):Sleep
Next
ScreenInfo scrw,scrh
progscreen(Peek(UInteger,memm)) = ImageCreate(scrw/Peek(UInteger,memm)+1,scrh)
Put (scrw/Peek(UInteger,memm),0),progscreen(Peek(UInteger,memm)),pset
Poke UInteger,memm+64+(Peek(UInteger,memm)*8)+5,progscreen(Peek(UInteger,memm)+1)
For c = 1 To Peek(UInteger,memm)
	ImageDestroy(progscreen(c-1))
	progscreen(c-1) = ImageCreate(scrw/Peek(UInteger,memm),scrh)
	Poke UInteger,memm+64+(c*8)+5,progscreen(c)
	Poke UInteger,memm+64+(c*8)+6,scrw/Peek(UInteger,memm)*scrh
Next
Seek 108,1
Get #108,,shortstr
Poke String,memm+64+8+(Peek(UInteger,memm)-1*8)+4,shortstr
Poke UInteger, memm+64+8+(Peek(UInteger,memm)-1*8)+1,c
Rem Poke UInteger, memm+64+8+(Peek(UInteger,memm)-1*8),(Peek(UInteger,(memm+64+(Peek(UInteger,memm)*8)))+Peek(UInteger,(memm+64+(Peek(UInteger,memm)*8)+1)))+1
Poke UInteger, memm+64+8+(Peek(UInteger,memm)-1*8)+2,0
Poke UInteger, memm+64+8+(Peek(UInteger,memm)-1*8)+3,0
Poke UInteger,memm,Peek(UInteger,memm)-1
If Peek(UInteger,memm) > 0 Then Poke UInteger,memm+64+8+(Peek(UInteger,memm)*8),memm+Peek(UInteger,memm+64+(Peek(UInteger,memm)*8)+1)
If Peek(UInteger,memm) = 0 Then Poke UInteger,memm+64+8+(Peek(UInteger,memm)*8),memm+320
Poke UInteger,memm,Peek(UInteger,memm)+1
Close #108
GoTo 15
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "&Peek(UInteger,memm+319)
Poke UInteger,memm+320,10
?"Address memm+319 after poking memm+320: "&Peek(UInteger,memm+319):Sleep:System
that also has the issue.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Post by fxm »

First code: My God, no comment!

For the second code:
'memm' is an Any Ptr, but you work with Uinteger data.
Think about it a bit with this slightly modified example from yours:

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "& hex(Peek(UInteger,memm+319))
Poke UInteger,memm+320,&h12345678
?"Address memm+320 after poking memm+320: "& hex(Peek(UInteger,memm+320))
?"Address memm+319 after poking memm+320: "& hex(Peek(UInteger,memm+319))
Sleep
  • Code: Select all

    Memm Is the allocated memory pointer.
    Address memm+319 before poking memm+320: 0
    Address memm+320 after poking memm+320: 12345678
    Address memm+319 after poking memm+320: 34567800
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

fxm wrote:First code: My God, no comment!

For the second code:
'memm' is an Any Ptr, but you work with Uinteger data.
Think about it a bit with this slightly modified example from yours:

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "& hex(Peek(UInteger,memm+319))
Poke UInteger,memm+320,&h12345678
?"Address memm+320 after poking memm+320: "& hex(Peek(UInteger,memm+320))
?"Address memm+319 after poking memm+320: "& hex(Peek(UInteger,memm+319))
Sleep
  • Code: Select all

    Memm Is the allocated memory pointer.
    Address memm+319 before poking memm+320: 0
    Address memm+320 after poking memm+320: 12345678
    Address memm+319 after poking memm+320: 34567800
THANK YOU!
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

fxm wrote:First code: My God, no comment!

For the second code:
'memm' is an Any Ptr, but you work with Uinteger data.
Think about it a bit with this slightly modified example from yours:

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "& hex(Peek(UInteger,memm+319))
Poke UInteger,memm+320,&h12345678
?"Address memm+320 after poking memm+320: "& hex(Peek(UInteger,memm+320))
?"Address memm+319 after poking memm+320: "& hex(Peek(UInteger,memm+319))
Sleep
  • Code: Select all

    Memm Is the allocated memory pointer.
    Address memm+319 before poking memm+320: 0
    Address memm+320 after poking memm+320: 12345678
    Address memm+319 after poking memm+320: 34567800
it still doesn't work if I do this

Code: Select all

Poke UInteger,peek(uinteger,(peek(UInteger,memm)*8+64)+memm)+ c,d
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Post by fxm »

Username wrote:it still doesn't work if I do this

Code: Select all

Poke UInteger,peek(uinteger,(peek(UInteger,memm)*8+64)+memm)+ c,d
Same remark as for your first code:
I would not comment on this dreadful spaguetti of indirections.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Poke Modifying Previous Address' Value

Post by MrSwiss »

There are two possible ways, to eliminate both, peek & poke:
  • use a UInteger array
  • use a UInteger ptr
To simplify the code ...
by array(index) or by ptr[index].
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Poke Modifying Previous Address' Value

Post by dodicat »

position 320 is not a uinteger up from position 319, it is a byte up.
you could cast

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
#define mem cptr(uinteger ptr,memm)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "&Peek(uinteger,mem+319)
Poke uinteger,mem+320,123456789
?"Address memm+319 after poking memm+320: "&Peek(uinteger,memm+319)

print "poked value > "; peek(uinteger,mem+320)



sleep 
Or you could step properly

Code: Select all

Dim memm As any ptr
memm = allocate(1024 * 512)
var sz=sizeof(uinteger)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "& (Peek(UInteger,memm+319))
Poke UInteger,memm+319+sz,12345678

?"Address memm+320 after poking memm+320: "& (Peek(UInteger,memm+319+sz))
?"Address memm+319 after poking memm+320: "& (Peek(UInteger,memm+319))
Sleep 
Or the best is declare memm as uinteger ptr which saves all the fuss.
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Poke Modifying Previous Address' Value

Post by Username »

dodicat wrote:position 320 is not a uinteger up from position 319, it is a byte up.
you could cast

Code: Select all

Dim memm As Any ptr
memm = allocate(1024 * 512)
#define mem cptr(uinteger ptr,memm)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "&Peek(uinteger,mem+319)
Poke uinteger,mem+320,123456789
?"Address memm+319 after poking memm+320: "&Peek(uinteger,memm+319)

print "poked value > "; peek(uinteger,mem+320)



sleep 
Or you could step properly

Code: Select all

Dim memm As any ptr
memm = allocate(1024 * 512)
var sz=sizeof(uinteger)
?"Memm Is the allocated memory pointer."
?"Address memm+319 before poking memm+320: "& (Peek(UInteger,memm+319))
Poke UInteger,memm+319+sz,12345678

?"Address memm+320 after poking memm+320: "& (Peek(UInteger,memm+319+sz))
?"Address memm+319 after poking memm+320: "& (Peek(UInteger,memm+319))
Sleep 
Or the best is declare memm as uinteger ptr which saves all the fuss.
thank you
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Post by fxm »

The Peek/Poke keywords are coming from QuickBASIC which did not support pointers.
So this was the only way to read/write data of a given type from a specific address.

FreeBASIC supports pointers which with dereferencing ('*' or '[]' operator) allow to read/write at specific addresses.
This is the recommended method.


Otherwise, using:
Peek( [ datatype, ] address )
Poke [ datatype, ] address, value


With QuickBASIC, 'address' was a simple numeric value or an expression which followed an obvious arithmetic.

With FreeBASIC, 'address' can be an expression like 'p + n' where 'p' is a typed/untyped pointer and 'n' is a numerical value. The type of 'p' may be inconsistent with 'datatype'.
The use of such keywords with FreeBASIC can become unsafe when the arithmetic for calculating the address offset follows the arithmetic dedicated to the type of 'p', while the type of data read/written is of a different type.

Code: Select all

Dim As Integer Ptr p = Allocate(3 * Sizeof(Integer))

'' between the syntax using Poke / Peek (from QuickBASIC)

Poke Integer, p + 0, 1
Poke Integer, p + 1, 2
Poke Integer, p + 2, 3
Print Peek(Integer, p + 0)
Print Peek(Integer, p + 1)
Print Peek(Integer, p + 2)
Print

'' or this one dereferencing a pointer (FreeBASIC new feature)
'' (writings/readings to the same addresses)

p[0] = 4
p[1] = 5
p[2] = 6
Print p[0]
Print p[1]
Print p[2]
Print

'' there is no photo-finish on arrival

Sleep
Deallocate(p)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke Modifying Previous Address' Value

Post by fxm »

Still even with dynamic memory allocation, if the types of data to put there are disparate, defining a suitable structure by accessing it with a specific typed pointer is much safer:

Code: Select all

'' between the syntax using Poke / Peek (from QuickBASIC)

Dim As Any Ptr p = Allocate(Sizeof(Integer) + Sizeof(Short) + Sizeof(Byte))

Poke Integer, p + 0, 1
Poke Short, p + Sizeof(Integer), 2
Poke Byte, p + Sizeof(Integer) + Sizeof(Short), 3
Print Peek(Integer, p + 0)
Print Peek(Short, p + Sizeof(Integer))
Print Peek(Byte, p + Sizeof(Integer) + Sizeof(Short))
Print

Deallocate(p)

'' or this one defining a dedicated structure

Type myData
    Dim As Integer i
    Dim As Short s
    Dim As Byte b
End Type

Dim As myData Ptr pd = New myData

pd->i = 4
pd->s = 5
pd->b = 6
Print pd->i
Print pd->s
Print pd->b
Print

Delete pd

'' there is no photo-finish on arrival

Sleep
Post Reply