Poke example

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 227
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Poke example

Post by Löwenherz »

Anybody can Check this example? I wanted to find Out what is poke Statement? i know its built-in function for freebasic.. but how does its working?
Thanks

Code: Select all

' test for "what is poke" without built-in function ?
' anybody can check this code example if that's correct?
' thanks loewenherz
'
#Include "crt.bi"

#ifndef __crt_stddef_bi__
#define __crt_stddef_bi__

type ptrdiff_t as integer

#ifdef __FB_DOS__
	type size_t as ulong
	#ifndef ssize_t
	type ssize_t as long
	#endif
#else
	type size_t as uinteger
	#ifndef ssize_t
	type ssize_t as integer
	#endif
#endif

#ifndef wchar_t
#ifdef __FB_DOS__
	type wchar_t as ubyte
#elseif defined( __FB_WIN32__ ) or defined( __FB_CYGWIN__ )
	type wchar_t as ushort
#else
	type wchar_t as long
#endif
#endif

#ifndef wint_t
	type wint_t as wchar_t
#endif

#ifndef NULL
#define NULL 0
#endif

#endif


' mem.bi
'declare function memcpy (byval as any ptr, byval as const any ptr, byval as size_t) as any Ptr


SUB Poke1(address AS ANY PTR, value AS INTEGER)
    DIM AS INTEGER PTR valuePtr = @value
    memcpy(address, valuePtr, SIZEOF(INTEGER))
END SUB

' Example test
DIM SHARED AS INTEGER var1

var1 = 12345

Print "Original value of var: "; var1

Poke1(VARPTR(var1), 65) ' ASCII code for 'A'

Print "Value of var after Poke: "; var1

Sleep
Löwenherz
Posts: 227
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Poke example

Post by Löwenherz »

OK its Not so important how does These both functions are Working.. without built-in function .. my Idea was Peek came First to get the value of an Adress in memory and then Second using poke to assign this value to a Location in memory but thats the contrary way of going ;-) If you are using Peek First you will get a value 0 . below its a correct Handling

Code: Select all

' peek: Gets the value of an arbitrary type at an address in memory
' poke: Assigns a value to a location in memory.

' 1) -------------------------------- //

Dim As Integer v

' first poke..
Sub MyPoke (Byval po As Integer Ptr)
    Print po 'output: 2025
    Poke Integer, po, 2025
End Sub

' then peek..
Sub MyPeek (Byval pe As Integer Ptr)
    Print pe
    Print Peek(Integer, pe)
End sub

MyPoke(@v) '1703648
MyPeek(@v) '1703648
' output: 2025
Sleep

' 2) -------------------------------- //
Dim i3 As Integer, p2 As Integer Ptr
p2 = @i3

Poke Integer, p2, 2025
Print Peek(Integer, p2)
'output 2025
Sleep
fxm
Moderator
Posts: 12467
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Poke example

Post by fxm »

Since 'Peek()' returns by reference, 'Peek()' can also be used to assign a value to a location in memory instead of 'Poke()':

Code: Select all

Dim As Integer value = 1234

Print Peek(Integer, @value)
Peek(Integer, @value) = 5678  '' instead of Poke Integer, @value, 5678
Print Peek(Integer, @value)
Sleep
Post Reply